1. 引言
本节为非规范性内容。
图形处理单元(GPU)在个人计算领域中已成为实现丰富渲染和计算应用的关键。WebGPU 是一个为 Web 暴露 GPU 硬件能力的 API。该 API 从零开始设计,能够高效映射到(2014年之后的)原生 GPU API。WebGPU 与 WebGL 无关,也不直接针对 OpenGL ES。
WebGPU 将物理 GPU 硬件视为 GPUAdapter。它通过
GPUDevice
提供与适配器的连接,设备负责管理资源,并通过设备的 GPUQueue
执行命令。GPUDevice
可能拥有自己的高速存储器以供处理单元访问。GPUBuffer 和
GPUTexture
是由 GPU 内存支持的物理资源。GPUCommandBuffer
和 GPURenderBundle
是用户录制命令的容器。GPUShaderModule
包含 着色器代码。其他资源如 GPUSampler 或
GPUBindGroup
用于配置 GPU 如何使用物理资源。
GPU 通过 GPUCommandBuffer
编码的命令,通过 管线
执行,该管线是固定功能与可编程阶段的混合体。可编程阶段运行着色器,即专为 GPU 硬件设计的特殊程序。管线的大部分状态由 GPURenderPipeline
或 GPUComputePipeline
对象定义。未包含在这些 管线对象中的状态,则在命令编码阶段通过如 beginRenderPass()
或 setBlendConstant()
之类命令设置。
2. 恶意使用考量
本节为非规范性内容。 介绍了在 Web 上暴露此 API 所带来的风险。
2.1. 安全考量
WebGPU 的安全要求与 Web 一贯的要求相同,也是不可协商的。总体原则是:在所有命令到达 GPU 之前进行严格校验,确保页面只能操作自身数据。
2.1.1. 基于 CPU 的未定义行为
WebGPU 实现会将用户发起的工作量转换为目标平台特定的 API 命令。原生 API 规定了命令的有效用法(例如见 vkCreateDescriptorSetLayout),并且通常不保证不遵守有效用法规则时的任何结果。这被称为“未定义行为”,攻击者可以利用它访问非授权内存,或迫使驱动执行任意代码。
为禁止不安全用法,WebGPU 针对任意输入都定义了允许的行为范围。实现必须校验所有用户输入,只允许有效工作负载到达驱动。本规范规定了所有错误条件及其处理语义。例如,在 copyBufferToBuffer() 的 "source" 和 "destination"
同时指定同一缓冲区,且区间相交时,GPUCommandEncoder
会生成错误,且不会执行其它操作。
更多错误处理信息参见 § 22 错误与调试。
2.1.2. 基于 GPU 的未定义行为
WebGPU 着色器 由 GPU 内部的计算单元执行。在原生 API
中,某些着色器指令可能在 GPU 上导致未定义行为。为应对这一点,WebGPU 严格定义了着色器指令集及其行为。当为 createShaderModule()
提供着色器时,WebGPU 实现必须在进行任何平台特定着色器转换或变换之前进行校验。
2.1.3. 未初始化数据
一般来说,分配新内存可能暴露系统上其它应用残留的数据。为避免此问题,WebGPU 在概念上会将所有资源初始化为零,尽管若实现检测到开发者已手动初始化内容时可跳过此步骤。这包括着色器内变量和共享工作组内存。
清除工作组内存的具体机制依平台而异。若原生 API 未提供清除机制,WebGPU 实现会先在所有调用中清空,再同步,然后继续执行开发者代码。
GPULoadOp
"load"
改为 "clear")。
因此,无论实现是否有性能损耗,所有实现应当在开发者控制台发出警告。
2.1.4. 着色器中的越界访问
着色器可以直接访问物理资源(如 "uniform"
GPUBufferBinding),也可以通过纹理单元(为纹理坐标转换而设的固定功能硬件块)间接访问。WebGPU API 的校验只能保证所有着色器输入已提供且类型正确。如果未通过纹理单元访问,WebGPU API 无法保证数据访问不会越界。
为防止着色器访问非本应用 GPU 内存,WebGPU 实现可在驱动中启用“健壮缓冲区访问”模式,确保访问不会超出缓冲区界限。
或者,实现可在着色器代码中插入手动越界检查。此时,越界检查仅适用于数组索引,对结构体字段访问则无需手动检查,因为主机侧 minBindingSize
校验已覆盖。
若着色器尝试读取物理资源边界外的数据,实现可以:
-
返回资源边界内其他位置的值
-
返回值向量 "(0, 0, 0, X)",其中 X 可为任意值
-
部分丢弃绘制或调度调用
若着色器尝试写入物理资源边界外,实现可以:
-
写入资源边界内其他位置
-
丢弃写入操作
-
部分丢弃绘制或调度调用
2.1.5. 无效数据
从 CPU 向 GPU 上传 浮点数数据或在 GPU 上生成浮点数时,可能会出现不对应于有效数值的二进制表示(如无穷大或 NaN)。此时 GPU 行为取决于硬件对 IEEE-754 标准的实现精度。WebGPU 保证引入无效浮点数只会影响算术运算结果,不会有其他副作用。
2.1.6. 驱动漏洞
GPU 驱动程序和其他软件一样可能存在漏洞。攻击者可能利用驱动的错误行为访问非授权数据。为降低风险,WebGPU 工作组将与 GPU 厂商协作,将 WebGPU 一致性测试套件(CTS)集成到驱动测试流程中,类似 WebGL 的做法。WebGPU 实现应对已发现的部分漏洞有兼容性处理,并对无法绕过的已知漏洞驱动禁用 WebGPU。
2.1.7. 时序攻击
2.1.7.1. 内容时间线时序
WebGPU 不会向 JavaScript 暴露在 内容时间线上由 agent 在 agent cluster
中共享的新状态。内容时间线状态如 [[mapping]]
仅在显式 内容时间线任务(如普通
JavaScript)中变化。
2.1.7.2. 设备/队列时间线时序
可写存储缓冲区和其他跨调用通信机制可能用于在队列时间线上构造高精度定时器。
可选的 "timestamp-query"
特性也为 GPU 操作提供高精度计时。为缓解安全和隐私风险,定时查询的值被对齐到较低精度:参见 current queue timestamp。特别注意:
-
设备时间线通常在多个源共享的进程中运行,因此跨源隔离(如 COOP/COEP)无法隔离设备/队列时间线定时器。
-
队列时间线工作由设备时间线发起,可能在不能像 CPU 进程(如 Meltdown 缓解)那样隔离的 GPU 硬件上执行。
-
GPU 硬件通常不易受到 Spectre 类攻击,但 WebGPU 可能在软件中实现,软件实现可能运行在共享进程中,无法依赖隔离缓解。
2.1.8. Row hammer 攻击
Row hammer 是一类利用 DRAM 单元状态泄漏的攻击,可被用于 GPU。WebGPU 没有专门的防护措施,依赖于平台级解决方案,例如缩短内存刷新间隔。
2.1.9. 拒绝服务
WebGPU 应用可访问 GPU 内存和计算单元。WebGPU 实现可以限制应用可用的 GPU 内存,以保证其他应用的响应性。对于 GPU 处理时间,WebGPU 实现可以设置“看门狗”定时器,确保应用不会导致 GPU 无响应超过数秒。这些措施类似于 WebGL 所采用的策略。
2.1.10. 工作负载识别
WebGPU 提供对全局受限资源的访问,这些资源由同一台机器上不同程序(和网页)共享。应用可通过间接探测全局资源受限程度,进而推测其他网页正在进行的工作负载。这些问题与 Javascript 中的系统内存和 CPU 执行吞吐量类似。WebGPU 没有对此提供额外的缓解措施。
2.1.11. 内存资源
WebGPU 允许从机器全局内存堆(如 VRAM)进行可失败的分配。这使得应用可以通过尝试分配并观察分配失败情况,探测系统剩余可用内存(针对特定堆类型)。
GPU 内部一般有一个或多个(通常只有两个)所有运行应用共享的内存堆。当某个堆耗尽时,WebGPU 创建资源会失败。这是可观察到的,可能使恶意应用推测出其他应用使用了哪些堆,以及它们分配了多少。
2.1.12. 计算资源
如果一个站点与另一个站点同时使用 WebGPU,它可能会观察到处理某些工作的耗时增加。例如,持续向队列提交计算工作并跟踪完成时间,可能会发现其他任务也开始使用 GPU。
GPU 有多个可独立测试的部分,如算术单元、纹理采样单元、原子单元等。恶意应用可以检测某些单元的负载,并试图分析压力模式来猜测其他应用的工作负载。这与 Javascript 的 CPU 执行现实类似。
2.1.13. 能力滥用
恶意站点可能滥用 WebGPU 所暴露的能力,运行对用户或其体验无益、仅对站点有利的计算。例如隐蔽挖矿、密码破解或彩虹表计算。
无法针对这类 API 使用方式加以防范,因为浏览器无法区分有效负载和滥用负载。这是 Web 上所有通用计算能力(如 JavaScript、WebAssembly 或 WebGL)面临的普遍问题。WebGPU 只是让某些负载的实现更容易,或比 WebGL 更高效。
为缓解此类滥用,浏览器可对后台标签页的操作进行限速,可警告标签页资源占用过高,并可限制哪些上下文允许使用 WebGPU。
用户代理可基于启发式方法向用户发出高功耗警告,尤其在检测到潜在恶意使用时。如果实现此类警告,应将 WebGPU 使用纳入与 JavaScript、WebAssembly、WebGL 等相同的启发式中。
2.2. 隐私考量
WebGPU 的隐私考量与 WebGL 类似。GPU API 十分复杂,必须出于必要暴露设备能力的各个方面,以便开发者能够有效利用这些能力。一般的缓解方法是对潜在可识别信息进行归一化或分桶,并在可能的情况下强制行为一致。
用户代理不得暴露超过 32 种可区分配置或分桶。
2.2.1. 机器特有功能与限制
WebGPU 可以揭示底层 GPU 架构和设备结构的许多细节,包括可用的物理适配器、GPU 和 CPU 资源的多项限制(如最大纹理尺寸),以及可用的任何可选硬件专属能力。
用户代理没有义务暴露真实硬件限制,可以完全控制机器细节的暴露程度。减少指纹识别的一种策略是将所有目标平台归为少数几个分桶。总体来说,暴露硬件限制的隐私影响与 WebGL 相同。
默认限制值也被故意设置得足够高,以便大多数应用无需请求更高限制即可运行。所有 API 的使用都按请求限制进行校验,因此不会因意外而向用户暴露实际硬件能力。
2.2.2. 机器特有产物
和 WebGL 一样,可以观察到一些机器特有的光栅化/精度差异和性能差异。这涉及光栅化覆盖及模式、着色器阶段间 varyings 的插值精度、计算单元调度,以及更多执行相关特性。
通常,同一厂商的大多数或全部设备的光栅化和精度指纹是相同的。性能差异难以完全规避,但信号强度较低(如 JS 执行性能)。
对隐私要求高的应用和用户代理应采用软件实现以消除这类产物。
2.2.3. 机器特有性能
通过测量 GPU 上特定操作的性能也是区分用户的一个因素。即便计时精度较低,重复执行某一操作也能体现用户机器在特定负载下的快慢。这是常见的识别向量(WebGL 和 Javascript 皆有),但信号较低且难以完全规避。
WebGPU 计算管线让开发者可绕过固定功能硬件直接访问 GPU,这带来了独特设备指纹识别的额外风险。用户代理可通过将逻辑 GPU 调用与实际计算单元解耦来降低此风险。
2.2.4. 用户代理状态
本规范未为源定义任何额外用户代理状态。但预期用户代理会对 GPUShaderModule、
GPURenderPipeline、
GPUComputePipeline
等高开销编译结果进行缓存。这些缓存有助于提升 WebGPU 应用首次访问后的加载速度。
对规范而言,这些缓存与极快编译无异,但应用可以轻易测量 createComputePipelineAsync()
的耗时。这可能导致跨源信息泄露(如“用户是否访问过包含特定着色器的站点”),因此用户代理应遵循 存储分区最佳实践。
系统的 GPU 驱动也可能有自己的着色器和管线编译缓存。用户代理可在可能的情况下禁用该缓存,或为着色器添加分区数据,使驱动将其视为不同对象。
2.2.5. 驱动漏洞
除安全考量中提到的问题外,驱动漏洞还可能导致行为差异,成为区分用户的手段。此处可采用安全考量中提及的缓解措施,如与 GPU 厂商协作,在用户代理中实现已知问题的兼容处理。
2.2.6. 适配器标识符
WebGL 实践表明,开发者有合理需求识别其代码运行的 GPU,以便创建和维护健壮的 GPU 内容。例如识别有已知驱动漏洞的适配器,以便规避或避免在某些硬件上使用表现不佳的特性。
但暴露适配器标识符自然会增加指纹识别信息量,因此有必要限制适配器识别的精度。
可以采取多项缓解措施以平衡内容健壮性与隐私保护。首先,用户代理可通过主动识别和规避已知驱动问题,减少开发者负担,就像浏览器开始用 GPU 以来所做的一样。
默认暴露适配器标识符时应尽量宽泛,只要有用即可。例如,可仅标识适配器厂商及架构而非具体型号。有时也可报告实际适配器的合理代理的标识符。
在需要完整详细适配器信息(如提交 bug 报告)时,可请用户同意向页面披露更多硬件信息。
最后,若用户代理认为合适(如增强隐私模式下),可完全不报告适配器标识符。
3. 基础
3.1. 约定
3.1.1. 语法速记
本规范中使用了如下语法速记:
.(点)语法,常见于编程语言。-
短语“
Foo.Bar”表示“值(或接口)Foo的Bar成员”。如果Foo是 有序映射(ordered map)且Bar在Foo中未存在,则返回undefined。 ?.(可选链)语法,借鉴自 JavaScript。-
短语“
Foo?.Bar”表示:“如果Foo为null或undefined或Bar在Foo中未存在,则为undefined;否则为Foo.Bar”。例如,若
buffer是一个GPUBuffer,则buffer?.\[[device]].\[[adapter]]表示:“如果buffer为null或undefined,则为undefined;否则为buffer的\[[device]]内部槽的\[[adapter]]内部槽。” ??(空值合并)语法,借鉴自 JavaScript。-
短语“
x??y”表示:“如果x不为 null 或 undefined,则为x,否则为y。” - 槽支持属性(slot-backed attribute)
-
由同名内部槽支持的 WebIDL 属性。其可变性视规范而定。
3.1.2. WebGPU 对象
WebGPU 接口定义了WebGPU 对象的公共接口和状态。它可在其创建的内容时间线上使用,此时它是 JavaScript 暴露的 WebIDL 接口。
任何包含 GPUObjectBase 的接口都是 WebGPU 接口。
内部对象跟踪WebGPU 对象在设备时间线上的状态。所有对内部对象可变状态的读写都发生在单一有序的设备时间线步骤中。
在WebGPU 对象上可定义以下特殊属性类型:
- 不可变属性(immutable property)
-
在对象初始化时设定的只读槽,可从任意时间线访问。
注:由于该槽不可变,实现可在多个时间线中保有副本,按需分配。不可变属性这样定义是为避免在规范中描述多个副本。
- 内容时间线属性(content timeline property)
-
仅可从对象创建的内容时间线访问的属性。
- 设备时间线属性(device timeline property)
-
跟踪内部对象状态,仅可从对象创建的设备时间线访问。设备时间线属性可为可变的。
设备时间线属性命名为
[[带括号]],为内部槽。 - 队列时间线属性(queue timeline property)
-
跟踪内部对象状态,仅可从对象创建的队列时间线访问。队列时间线属性可为可变的。
队列时间线属性命名为
[[带括号]],为内部槽。
interface mixin GPUObjectBase {attribute USVString label ; };
GPUObjectBase
parent,接口 T,GPUObjectDescriptorBase
descriptor)(其中 T 继承自 GPUObjectBase),请在内容时间线上执行以下步骤:
-
令 device 为 parent.
[[device]]。 -
令 object 为 T 的新实例。
-
设置 object.
[[device]]为 device。 -
返回 object。
GPUObjectBase
具有如下不可变属性:
GPUObjectBase
具有如下内容时间线属性:
label,类型为 USVString-
开发者提供的标签,由实现自定义使用。可由浏览器、操作系统或其他工具用于帮助开发者识别底层内部对象。示例包括在
GPUError消息、控制台警告、浏览器开发者工具和平台调试工具中显示标签。注:实现应当利用标签提升错误消息的可读性,用于标识 WebGPU 对象。但这不必是标识对象的唯一方式:实现还应结合其他可用信息,尤其是在没有标签时。例如:
-
打印
GPUTexture视图时,显示其父对象的标签。 -
打印
GPURenderPassEncoder或GPUComputePassEncoder时,显示其父GPUCommandEncoder的标签。 -
打印
GPUCommandBuffer时,显示其源GPUCommandEncoder的标签。 -
打印
GPURenderBundle时,显示其源GPURenderBundleEncoder的标签。
注:label是GPUObjectBase的属性。两个GPUObjectBase“包装器”对象即使引用同一底层对象,其 label 状态也是完全独立的(如由getBindGroupLayout()返回时)。除非通过 JavaScript 设置,否则label属性不会变更。这意味着一个底层对象可关联多个标签。本规范未定义标签如何传递到 设备时间线。标签的使用完全由实现自定义:错误消息可显示最近设置的标签、所有已知标签,或完全不显示标签。
属性类型为
USVString,因为某些用户代理可能将其传递给底层原生 API 的调试工具。 -
GPUObjectBase
具有如下设备时间线属性:
[[valid]],类型为boolean,初始值为true-
若为
true,表示该内部对象可用。
[[device]])被垃圾回收。然而,这无法保证,因为某些实现可能需要强引用父对象。
因此,开发者应假定 WebGPU 接口在所有子对象被回收前可能不会被垃圾回收。这可能导致部分资源比预期保留更长时间。
如果需要可预测地释放分配的资源,应优先调用 destroy 方法(如 GPUDevice.destroy()
或 GPUBuffer.destroy()),而不是依赖垃圾回收。
3.1.3. 对象描述符
对象描述符用于保存创建对象所需的信息,通常通过 GPUDevice 的 create*
方法之一进行对象创建。
dictionary {GPUObjectDescriptorBase USVString label = ""; };
GPUObjectDescriptorBase
具有如下成员:
label,类型为 USVString,默认值为""-
GPUObjectBase.label的初始值。
3.2. 异步性
3.2.1. 无效的内部对象与传染性无效
WebGPU 中的对象创建操作不会返回 Promise,但其本质上是异步的。返回的对象引用的是在设备时间线上被操作的内部对象。多数在设备时间线上发生的错误不会通过异常或拒绝抛出,而是通过关联设备上生成的 GPUError 进行传递。
内部对象可以是有效或无效。无效对象不会在之后变为有效,但部分有效对象可以被使无效(invalidated)。
如果对象无法被创建,则其从创建开始即为无效。例如,对象描述符未能描述出有效对象,或者没有足够内存分配资源时会发生此情况。如果从另一个无效对象创建对象(例如对无效的 GPUTexture 调用
createView()),也会如此。这类情况称为传染性无效。
大多数类型的内部对象在创建后不会变为无效,但仍可能变得不可用,例如其所属设备丢失或destroyed,或对象处于特殊内部状态(如缓冲区状态“destroyed”)。
部分类型的内部对象在创建后可以变为无效;具体包括设备、适配器、GPUCommandBuffer,以及命令/通道/捆绑编码器。
GPUObjectBase
object,若满足以下设备时间线步骤的所有要求,则其可与 targetObject 一同使用(valid to use with):
-
object.
[[valid]]必须为true。 -
object.
[[device]].[[valid]]必须为true。 -
object.
[[device]]必须等于 targetObject.[[device]]。
3.2.2. Promise 顺序
WebGPU 中有若干操作会返回 promise。
WebGPU 不保证这些 promise 的 settle(resolve 或 reject)顺序,除非有如下例外:
-
对于某个
GPUQueueq,如果 p1 = q.onSubmittedWorkDone()先于 p2 = q.onSubmittedWorkDone()被调用,则 p1 必须早于 p2 settle。 -
对于某个
GPUQueueq 和同一GPUDevice上的GPUBufferb,如果 p1 = b.mapAsync()先于 p2 = q.onSubmittedWorkDone()被调用,则 p1 必须早于 p2 settle。
应用不得依赖于其它 promise 的 settle 顺序。
3.3. 坐标系统
渲染操作使用如下坐标系统:
-
归一化设备坐标(NDC)有三维:
-
-1.0 ≤ x ≤ 1.0
-
-1.0 ≤ y ≤ 1.0
-
0.0 ≤ z ≤ 1.0
-
左下角为 (-1.0, -1.0, z)。
归一化设备坐标。 注:
z = 0还是z = 1作为近平面由应用决定。上图以z = 0为近平面,但实际行为由着色器使用的投影矩阵、depthClearValue和depthCompare函数共同决定。 -
-
裁剪空间坐标有四维:(x, y, z, w)
-
帧缓冲坐标用于寻址帧缓冲内的像素。
-
为二维坐标。
-
每个像素在 x、y 方向上长度为 1。
-
左上角为 (0.0, 0.0)。
-
x 向右递增。
-
y 向下递增。
-
参见 § 17 渲染通道 和 § 23.2.5 光栅化。
帧缓冲坐标。 -
-
视口坐标结合了 x、y 方向的帧缓冲坐标与 z 方向的深度。
-
通常 0.0 ≤ z ≤ 1.0,但可通过
[[viewport]].minDepth和maxDepth,以及setViewport()修改。
-
-
片元坐标与视口坐标一致。
-
纹理坐标,在 2D 中有时称为“UV 坐标”,用于采样纹理,其分量数量与
texture dimension匹配。-
0 ≤ u ≤ 1.0
-
0 ≤ v ≤ 1.0
-
0 ≤ w ≤ 1.0
-
(0.0, 0.0, 0.0) 位于纹理存储顺序的首个 texel。
-
(1.0, 1.0, 1.0) 位于纹理存储顺序的最后一个 texel。
二维纹理坐标。 -
-
窗口坐标,或称显示坐标,与帧缓冲坐标一致,用于与外部显示或类似接口交互时。
注:WebGPU 的坐标系统与 DirectX 图形管线中的坐标系统一致。
3.4. 编程模型
3.4.1. 时间线
WebGPU 的行为以“时间线”为单位进行描述。每个操作(以算法定义)都发生在某个时间线上。时间线明确了操作的顺序,以及哪些状态可被哪些操作访问。
注: 这种“时间线”模型反映了浏览器引擎多进程模型(如“内容进程”和“GPU 进程”)的约束,也体现了许多实现中 GPU 作为独立执行单元的现实。实现 WebGPU 并不要求时间线并行执行,因此不要求多进程,甚至不要求多线程。(但如 获取上下文图像内容副本 这类需同步阻塞其它时间线完成的情况,仍需支持并发。)
- 内容时间线
-
与 Web 脚本的执行相关。包括本规范描述的所有方法调用。
要从
GPUDevicedevice的操作向内容时间线派发步骤,可通过 为 GPUDevice 排队全局任务。 - 设备时间线
-
与用户代理发起的 GPU 设备操作相关。包括适配器、设备、GPU 资源与状态对象的创建,这些操作从用户代理控制 GPU 的部分看通常是同步的,但可在独立的操作系统进程中运行。
- 队列时间线
-
与 GPU 计算单元上操作的执行相关。包括实际在 GPU 上运行的绘制、拷贝和计算任务。
- 时间线无关
-
可以与上述任意时间线关联。
如仅操作不可变属性或调用步骤传入的参数,则可派发到任意时间线。
- 不可变值示例术语定义
-
可用于任意时间线。
- 内容时间线示例术语定义
-
仅可用于内容时间线。
- 设备时间线示例术语定义
-
仅可用于设备时间线。
- 队列时间线示例术语定义
-
仅可用于队列时间线。
本规范中,当返回值依赖于非内容时间线上发生的工作时,采用异步操作。API 以 promise 或事件的形式表现异步操作。
GPUComputePassEncoder.dispatchWorkgroups()
示例:
-
用户在 内容时间线 上通过
GPUComputePassEncoder的方法编码dispatchWorkgroups命令。 -
用户调用
GPUQueue.submit(),将GPUCommandBuffer提交给用户代理,用户代理在 设备时间线 上调用操作系统驱动进行底层提交。 -
GPU 调度器在 队列时间线 上将提交分派到实际计算单元执行。
GPUDevice.createBuffer()
示例:
-
用户填写
GPUBufferDescriptor,并用其创建GPUBuffer,在 内容时间线 上发生。 -
用户代理在 设备时间线 上创建底层缓冲区。
3.4.2. 内存模型
本节为非规范性内容。
一旦应用初始化过程中获取到 GPUDevice,我们可以将
WebGPU 平台描述为包含以下各层:
-
实现本规范的用户代理。
-
为该设备提供底层原生 API 驱动的操作系统。
-
实际的 CPU 和 GPU 硬件。
WebGPU 平台的每一层可能有不同类型的内存,用户代理在实现规范时需要加以考虑:
-
脚本拥有的内存,如脚本创建的
ArrayBuffer,通常无法被 GPU 驱动直接访问。 -
用户代理可能有不同进程分别负责内容运行和与 GPU 驱动的通信。在这种情况下,会使用进程间共享内存来传递数据。
-
独立显卡有自己的高带宽内存,而集成显卡通常与系统共享内存。
大多数物理资源分配在适合 GPU 计算或渲染的内存类型中。当用户需要向 GPU 提供新数据时,数据可能首先需要跨越进程边界,抵达与 GPU 驱动通信的用户代理部分。随后可能需要使数据对驱动可见,有时这需要拷贝到驱动分配的中转(staging)内存。最后,数据还可能需要传输到独立 GPU 内存中,并在内部转换为最适合 GPU 运算的布局。
所有这些转换都由用户代理的 WebGPU 实现负责完成。
注:上述示例描述了最坏情况,实际实现中可能无需跨越进程边界,或者能直接向用户暴露由驱动管理的内存(例如通过
ArrayBuffer),从而避免数据拷贝。
3.4.3. 资源用法
- 输入(input)
-
为 draw 或 dispatch 调用提供输入数据的缓冲区。会保留内容。由缓冲区
INDEX、VERTEX或INDIRECT允许。 - 常量(constant)
-
从着色器视角为常量的资源绑定。会保留内容。由缓冲区
UNIFORM或纹理TEXTURE_BINDING允许。 - 存储(storage)
-
读/写存储资源绑定。由缓冲区
STORAGE或纹理STORAGE_BINDING允许。 - 存储只读(storage-read)
-
只读存储资源绑定。会保留内容。由缓冲区
STORAGE或纹理STORAGE_BINDING允许。 - 附件(attachment)
-
在渲染通道中作为读/写输出附件或仅写解决目标的纹理。由纹理
RENDER_ATTACHMENT允许。 - 附件只读(attachment-read)
-
在渲染通道中作为只读附件的纹理。会保留内容。由纹理
RENDER_ATTACHMENT允许。
子资源(subresource)指整个缓冲区或纹理子资源。
-
U 中每个用法都是 存储。
即使可写,也允许有多个此类用法。这称为存储用法例外(usage scope storage exception)。
-
U 中每个用法都是 附件。
即使可写,也允许有多个此类用法。这称为附件用法例外(usage scope attachment exception)。
强制要求用法仅可组合为兼容用法列表,使得 API 能够限制内存操作中的数据竞争出现时机。该属性使基于 WebGPU 的应用更容易无修改地在不同平台上运行。
-
作为深度/模板附件,所有方面都标记为只读(根据需要使用
depthReadOnly和/或stencilReadOnly)。 -
作为 draw 调用的纹理绑定。
-
一个缓冲区或纹理可作为存储绑定到渲染通道的两个不同 draw 调用。
-
单个缓冲区的不相交区间可分别作为存储绑定到两个不同绑定点。
重叠区间不得在单一 dispatch/draw 调用中绑定;这由“编码器绑定组别名可写资源”检查。
但同一切片不得作为两个不同附件重复绑定;这由 beginRenderPass()
检查。
3.4.4. 同步机制
用法范围(usage scope)是一个从有序映射,其键为子资源,值为 列表<内部用法>。每个用法范围覆盖一组可能并发执行的操作,因此在该范围内对子资源的使用只能是兼容用法列表。
-
对于 A 中的每一项 [subresource, usage]:
-
添加(Add) subresource 到 B,并指定 usage usage。
-
用法范围在编码期间被构建和校验:
用法范围如下:
-
在计算通道中,每个 dispatch 命令(
dispatchWorkgroups()或dispatchWorkgroupsIndirect())即为一个用法范围。若某子资源在当前 dispatch 调用中可能被访问,则视为在该用法范围内被使用,包括:
-
当前
GPUComputePipeline的[[layout]]所用绑定组(bind group)槽引用的全部子资源 -
dispatch 调用直接使用的缓冲区(如间接缓冲区)
注: 诸如 setBindGroup() 等状态设定命令本身不会直接将绑定资源计入用法范围,它们只改变 dispatch 命令检查的状态。
-
-
一次渲染通道(render pass)即为一个用法范围。
若某子资源在任何命令中被引用(包括状态设定命令,与计算通道不同),则视为在该用法范围内被使用,包括:
-
通过
setVertexBuffer()设置的缓冲区 -
通过
setIndexBuffer()设置的缓冲区 -
通过 setBindGroup() 设置的绑定组引用的所有子资源
-
draw 调用直接使用的缓冲区(如间接缓冲区)
-
注:拷贝命令为独立操作,不使用用法范围进行校验,其自身实现了防止自竞争的校验。
-
在渲染通道中,任何 setBindGroup() 调用所用的子资源,无论当前绑定的管线着色器/布局是否真的依赖这些绑定,或该绑定组是否被后续 set 覆盖。
-
任何
setVertexBuffer()调用所用缓冲区,无论是否有 draw 调用依赖该缓冲区,或该缓冲区是否被后续 set 覆盖。 -
任何
setIndexBuffer()调用所用缓冲区,无论是否有 draw 调用依赖该缓冲区,或该缓冲区是否被后续 set 覆盖。 -
作为颜色附件、解决附件或深度/模板附件用于
GPURenderPassDescriptor的纹理子资源,被beginRenderPass()使用,无论着色器是否真的依赖这些附件。 -
在绑定组条目中具有可见性 0,或仅对计算阶段可见但被用于渲染通道的资源(反之亦然)。
3.5. 核心内部对象
3.5.1. 适配器(Adapters)
适配器(adapter)标识系统上的 WebGPU 实现:既包括底层平台上的计算/渲染功能实例,也包括浏览器在该功能之上实现 WebGPU 的实例。
适配器通过 GPUAdapter
暴露。
适配器并不唯一代表底层实现:多次调用 requestAdapter()
每次都会返回不同的适配器对象。
每个 适配器对象只能用于创建一个 设备(device):一旦成功调用 requestDevice(),该适配器的
[[state]]
变为 "consumed"。此外,适配器对象可能在任何时刻过期(expire)。
注:
这样保证应用在创建设备时能使用最新的系统状态来选择适配器。也让多种场景(如首次初始化、因适配器移除重初始化、因 GPUDevice.destroy()
测试重初始化等)行为保持一致,提高健壮性。
若适配器以显著性能换取更广兼容性、更可预测行为或更好的隐私,则可视为回退适配器(fallback adapter)。不是每个系统都必须提供回退适配器。
[[features]],类型为 有序集合<GPUFeatureName>,只读-
可用于在此适配器上创建设备的特性。
[[limits]],类型为支持的限制,只读[[fallback]],类型为boolean,只读-
为
true时,表示该适配器是回退适配器。 [[xrCompatible]],类型为 boolean-
为
true时,表示该适配器请求了与 WebXR 会话 兼容。
[[state]],初始值为"valid"-
"valid"-
适配器可用于创建设备。
"consumed"-
适配器已被用于创建设备,不能再次使用。
"expired"-
适配器因其他原因已过期。
3.5.2. 设备(Devices)
设备(device)是适配器的逻辑实例,通过它可以创建内部对象。
一个设备独占其上创建的所有内部对象:当该设备变为无效(丢失或 销毁时),它本身和所有直接(如
createTexture())或间接(如
createView())创建的对象都会隐式变为不可用。
[[adapter]],类型为 适配器,只读-
创建该设备的适配器。
[[features]],类型为 有序集合<GPUFeatureName>,只读[[limits]],类型为支持的限制,只读
GPUDeviceDescriptor
descriptor 创建新设备,请执行以下设备时间线步骤:
-
令 features 为 descriptor.
requiredFeatures中值的有序集合。 -
如果 features 包含
"texture-formats-tier2":-
追加
"texture-formats-tier1"到 features。
-
-
如果 features 包含
"texture-formats-tier1":-
追加
"rg11b10ufloat-renderable"到 features。
-
-
追加
"core-features-and-limits"到 features。 -
令 limits 为所有值均为默认值的支持的限制对象。
-
对于 descriptor.
requiredLimits中每个 (key, value):-
如果 value 不为
undefined且 value 优于 limits[key]:-
设 limits[key] = value。
-
-
-
令 device 为新设备对象。
-
设 device.
[[adapter]]= adapter。 -
设 device.
[[features]]= features。 -
设 device.
[[limits]]= limits。 -
返回 device。
每当用户代理需要撤销设备访问权限时,会在该设备的设备时间线上调用 丢失设备(device, "unknown"),该操作可能优先于当前队列中的其他操作。
如果某操作失败且副作用可能导致设备上的对象状态可见变化或内部实现/驱动状态损坏,应当丢失该设备以防止这些变化被观察到。
注:
非应用主动发起的设备丢失(通过 destroy())时,用户代理应无条件向开发者发出警告,即使
lost
promise 已被处理。此类场景应极其罕见,并且该信号对开发者至关重要,因为大多数 WebGPU API 会尽量表现为“一切正常”以避免中断运行时流程:不会抛出校验错误,大多数 promise 正常 resolve
等。
-
在 device.
[[content device]]的内容时间线上执行: -
完成所有等待 device 变为丢失的未完成步骤。
注:丢失的设备不会再生成错误。参见 § 22 错误与调试。
则在 timeline 上执行 steps。
3.6. 可选功能
WebGPU 适配器和设备具备功能,这些功能描述了WebGPU在不同实现之间的差异, 通常是由于硬件或系统软件的限制。 一项功能可以是特性或限制。
用户代理不得透露超过32个可区分的配置或分组。
适配器的功能必须符合§ 4.2.1 适配器功能保证。
仅支持的功能可以在requestDevice()中请求;
请求不支持的功能会导致失败。
设备的功能在"一个新的设备"中确定,
通过从适配器的默认值(无特性和默认的支持的限制)开始,
并根据在requestDevice()中请求的功能添加功能。
这些功能无论适配器的功能如何均会被强制执行。
有关隐私方面的考虑,请参阅§ 2.2.1 机器特定的功能和限制。
3.6.1. 特性
特性是一组可选的 WebGPU 功能,并非所有实现都支持这些功能,通常是由于硬件或系统软件的限制所致。
所有特性都是可选的,但适配器会对其可用性做出一定保证 (参见§ 4.2.1 适配器功能保证)。
设备仅支持在创建时确定的那组特性(参见§ 3.6 可选功能)。 API 调用将根据这些特性(而非适配器的特性)进行校验:
-
以新方式使用已有的 API 接口通常会导致校验错误。
-
有几种类型的可选 API 接口:
-
使用新方法或枚举值时总是抛出
TypeError异常。 -
使用带有(正确类型)非默认值的新字典成员通常会导致校验错误。
-
使用新的 WGSL
enable指令时,总是会在createShaderModule()的过程中产生校验错误。
-
每个特性所启用的功能描述可参见特性索引。
注意: 启用特性未必是理想选择,可能会影响性能。 因此,为了提升不同设备和实现间的可移植性,应用程序通常只应请求实际需要的特性。
3.6.2. 限制
每个限制都是对设备上 WebGPU 使用的数值限制。
注意: 设置“更好”的限制未必是理想选择,因为这样可能会对性能产生影响。 因此,为了提升不同设备和实现之间的可移植性,应用程序通常只有在确实需要时才请求比默认值更好的限制。
每个限制都有一个默认值。
适配器始终保证支持默认值或更好的 (参见§ 4.2.1 适配器功能保证)。
设备仅支持在创建时确定的那组限制(参见§ 3.6 可选功能)。 API 调用根据这些限制进行校验(而非适配器的限制), 不允许“更好”或更差。
对于任何给定的限制,一些值是更好的。 更好的限制值始终放宽校验,从而使更多程序变得有效。 对于每个限制类别,“更好”的定义如下。
不同的限制具有不同的限制类别:
支持的限制 对象具有 WebGPU 定义的每个限制的值:
| 限制名称 | 类型 | 限制类别 | 默认值 |
|---|---|---|---|
maxTextureDimension1D
| GPUSize32
| 最大值 | 8192 |
创建dimension
"1d"的纹理时,
size.width
的最大允许值。
| |||
maxTextureDimension2D
| GPUSize32
| 最大值 | 8192 |
创建dimension
"2d"的纹理时,
size.width
和 size.height
的最大允许值。
| |||
maxTextureDimension3D
| GPUSize32
| 最大值 | 2048 |
创建dimension
"3d"的纹理时,
size.width、
size.height
和 size.depthOrArrayLayers 的最大允许值。
| |||
maxTextureArrayLayers
| GPUSize32
| 最大值 | 256 |
创建dimension
"2d"的纹理时,
size.depthOrArrayLayers
的最大允许值。
| |||
maxBindGroups
| GPUSize32
| 最大值 | 4 |
创建GPUPipelineLayout时,
bindGroupLayouts
中允许的GPUBindGroupLayouts的最大数量。
| |||
maxBindGroupsPlusVertexBuffers
| GPUSize32
| 最大值 | 24 |
同时使用的绑定组和顶点缓冲区槽位的最大数量,包括所有低于最大索引的空槽位。
在createRenderPipeline()
和绘制调用中验证。
| |||
maxBindingsPerBindGroup
| GPUSize32
| 最大值 | 1000 |
创建GPUBindGroupLayout时可用的绑定索引数量。
注意: 此限制具有规范性,但属于人为设定。
按默认绑定槽位限制,一个绑定组实际上无法使用1000个绑定,
但这允许 | |||
maxDynamicUniformBuffersPerPipelineLayout
| GPUSize32
| 最大值 | 8 |
在一个GPUPipelineLayout中,
所有为动态偏移的uniform buffer的GPUBindGroupLayoutEntry条目的最大数量。
详见绑定槽位限制说明。
| |||
maxDynamicStorageBuffersPerPipelineLayout
| GPUSize32
| 最大值 | 4 |
在一个GPUPipelineLayout中,
所有为动态偏移的storage buffer的GPUBindGroupLayoutEntry条目的最大数量。
详见绑定槽位限制说明。
| |||
maxSampledTexturesPerShaderStage
| GPUSize32
| 最大值 | 16 |
对于每个GPUShaderStage
stage,在一个GPUPipelineLayout中,
所有采样纹理的GPUBindGroupLayoutEntry条目的最大数量。
详见绑定槽位限制说明。
| |||
maxSamplersPerShaderStage
| GPUSize32
| 最大值 | 16 |
对于每个GPUShaderStage
stage,在一个GPUPipelineLayout中,
所有采样器的GPUBindGroupLayoutEntry条目的最大数量。
详见绑定槽位限制说明。
| |||
maxStorageBuffersPerShaderStage
| GPUSize32
| 最大值 | 8 |
对于每个GPUShaderStage
stage,在一个GPUPipelineLayout中,
所有存储缓冲区的GPUBindGroupLayoutEntry条目的最大数量。
详见绑定槽位限制说明。
| |||
maxStorageTexturesPerShaderStage
| GPUSize32
| 最大值 | 4 |
对于每个可能的GPUShaderStage
stage,
在一个GPUPipelineLayout中,
所有存储纹理类型GPUBindGroupLayoutEntry的最大数量。
详见绑定槽位限制说明。
|
|||
maxUniformBuffersPerShaderStage
|
GPUSize32
|
最大值 | 12 |
对于每个可能的GPUShaderStage
stage,
在一个GPUPipelineLayout中,
所有uniform buffer类型GPUBindGroupLayoutEntry的最大数量。
详见绑定槽位限制说明。
|
|||
maxUniformBufferBindingSize
|
GPUSize64
|
最大值 | 65536 字节 |
绑定类型为
GPUBindGroupLayoutEntry
,且entry.buffer?.type
为 "uniform"
时,
GPUBufferBinding.size
的最大值。
|
|||
maxStorageBufferBindingSize
|
GPUSize64
|
最大值 | 134217728 字节(128 MiB) |
绑定类型为
GPUBindGroupLayoutEntry
,且entry.buffer?.type
为 "storage"
或 "read-only-storage"
时,
GPUBufferBinding.size
的最大值。
|
|||
minUniformBufferOffsetAlignment
|
GPUSize32
|
对齐 | 256 字节 |
绑定类型为
GPUBindGroupLayoutEntry
,且entry.buffer?.type
为 "uniform"
时,
GPUBufferBinding.offset
以及 setBindGroup() 传入的动态偏移量所需的对齐要求。
|
|||
minStorageBufferOffsetAlignment
|
GPUSize32
|
对齐 | 256 字节 |
绑定类型为
GPUBindGroupLayoutEntry
,且entry.buffer?.type
为 "storage"
或 "read-only-storage"
时,
GPUBufferBinding.offset
以及 setBindGroup() 传入的动态偏移量所需的对齐要求。
|
|||
maxVertexBuffers
|
GPUSize32 |
最大值 | 8 |
创建GPURenderPipeline时,允许的buffers的最大数量。
|
|||
maxBufferSize |
GPUSize64 |
最大值 | 268435456 字节(256 MiB) |
创建GPUBuffer时,size的最大数值。
|
|||
maxVertexAttributes |
GPUSize32 |
最大值 | 16 |
创建GPURenderPipeline时,所有attributes在buffers中的总和的最大数量。
|
|||
maxVertexBufferArrayStride
|
GPUSize32 |
最大值 | 2048 字节 |
创建GPURenderPipeline时,arrayStride的最大允许值。
|
|||
maxInterStageShaderVariables
|
GPUSize32 |
最大值 | 16 |
| 阶段间通信(如顶点输出、片元输入)可用的输入或输出变量的最大数量。 | |||
maxColorAttachments |
GPUSize32 |
最大值 | 8 |
在
GPURenderPipelineDescriptor.fragment.targets、
GPURenderPassDescriptor.colorAttachments
和 GPURenderPassLayout.colorFormats
中允许的最大颜色附件数量。
|
|||
maxColorAttachmentBytesPerSample
|
GPUSize32
|
最大值 | 32 |
| 渲染管线输出数据中,跨所有颜色附件存储一个采样(像素或子像素)所需的最大字节数。 | |||
maxComputeWorkgroupStorageSize
|
GPUSize32
|
最大值 | 16384 字节 |
一个计算阶段GPUShaderModule入口点可用的workgroup存储的最大字节数。
|
|||
maxComputeInvocationsPerWorkgroup
|
GPUSize32
|
最大值 | 256 |
一个计算阶段GPUShaderModule入口点的workgroup_size各维度乘积的最大值。
|
|||
maxComputeWorkgroupSizeX
|
GPUSize32
|
最大值 | 256 |
一个计算阶段GPUShaderModule入口点的workgroup_size
X 维度的最大值。
|
|||
maxComputeWorkgroupSizeY
|
GPUSize32
|
最大值 | 256 |
一个计算阶段GPUShaderModule入口点的workgroup_size
Y 维度的最大值。
|
|||
maxComputeWorkgroupSizeZ
|
GPUSize32
|
最大值 | 64 |
一个计算阶段GPUShaderModule入口点的workgroup_size
Z 维度的最大值。
|
|||
maxComputeWorkgroupsPerDimension
|
GPUSize32
|
最大值 | 65535 |
dispatchWorkgroups(workgroupCountX, workgroupCountY, workgroupCountZ)的参数的最大值。
|
|||
3.6.2.1.
GPUSupportedLimits
GPUSupportedLimits
用于暴露适配器或设备的支持的限制。
参见 GPUAdapter.limits
和 GPUDevice.limits。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUSupportedLimits {readonly attribute unsigned long ;maxTextureDimension1D readonly attribute unsigned long ;maxTextureDimension2D readonly attribute unsigned long ;maxTextureDimension3D readonly attribute unsigned long ;maxTextureArrayLayers readonly attribute unsigned long ;maxBindGroups readonly attribute unsigned long ;maxBindGroupsPlusVertexBuffers readonly attribute unsigned long ;maxBindingsPerBindGroup readonly attribute unsigned long ;maxDynamicUniformBuffersPerPipelineLayout readonly attribute unsigned long ;maxDynamicStorageBuffersPerPipelineLayout readonly attribute unsigned long ;maxSampledTexturesPerShaderStage readonly attribute unsigned long ;maxSamplersPerShaderStage readonly attribute unsigned long ;maxStorageBuffersPerShaderStage readonly attribute unsigned long ;maxStorageTexturesPerShaderStage readonly attribute unsigned long ;maxUniformBuffersPerShaderStage readonly attribute unsigned long long ;maxUniformBufferBindingSize readonly attribute unsigned long long ;maxStorageBufferBindingSize readonly attribute unsigned long ;minUniformBufferOffsetAlignment readonly attribute unsigned long ;minStorageBufferOffsetAlignment readonly attribute unsigned long ;maxVertexBuffers readonly attribute unsigned long long ;maxBufferSize readonly attribute unsigned long ;maxVertexAttributes readonly attribute unsigned long ;maxVertexBufferArrayStride readonly attribute unsigned long ;maxInterStageShaderVariables readonly attribute unsigned long ;maxColorAttachments readonly attribute unsigned long ;maxColorAttachmentBytesPerSample readonly attribute unsigned long ;maxComputeWorkgroupStorageSize readonly attribute unsigned long ;maxComputeInvocationsPerWorkgroup readonly attribute unsigned long ;maxComputeWorkgroupSizeX readonly attribute unsigned long ;maxComputeWorkgroupSizeY readonly attribute unsigned long ;maxComputeWorkgroupSizeZ readonly attribute unsigned long ; };maxComputeWorkgroupsPerDimension
3.6.2.2.
GPUSupportedFeatures
GPUSupportedFeatures
是一个 类似集合(setlike) 接口。它的
集合条目
是适配器或设备支持的 GPUFeatureName
值。
它只能包含 GPUFeatureName
枚举中的字符串。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUSupportedFeatures {readonly setlike <DOMString >; };
GPUSupportedFeatures
的 集合条目
类型为 DOMString,
这样可以让用户代理优雅地处理后续规范修订中新增但用户代理尚未识别的有效 GPUFeatureName。
如果 集合条目 的类型是
GPUFeatureName,如下代码将抛出
TypeError,
而不是返回 false:
3.6.2.3.
WGSLLanguageFeatures
WGSLLanguageFeatures
是
navigator.gpu.
的
类似集合(setlike) 接口。
它的 集合条目
是该实现支持的 WGSL 语言扩展 的字符串名称(不论适配器或设备)。
wgslLanguageFeatures
[Exposed =(Window ,Worker ),SecureContext ]interface WGSLLanguageFeatures {readonly setlike <DOMString >; };
3.6.2.4.
GPUAdapterInfo
GPUAdapterInfo
用于暴露关于适配器的各种标识信息。
GPUAdapterInfo
的成员不保证有任何特定值;如果没有提供值,该属性将返回空字符串
""。是否揭示这些值由用户代理自行决定,并且某些设备上可能所有值都为空。因此,应用必须能够处理任何可能的
GPUAdapterInfo
值,包括这些值缺失的情况。
适配器的 GPUAdapterInfo
可通过 GPUAdapter.info
和 GPUDevice.adapterInfo
获取。
这些信息是不可变的:对于某个适配器,每次访问同一个 GPUAdapterInfo
属性都会返回相同的值。
注意:
虽然 GPUAdapterInfo
属性一旦访问就不可变,
但实现可以在首次访问前延后决定每个属性的内容。
注意:
即便其他 GPUAdapter
实例代表同一物理适配器,
它们在 GPUAdapterInfo
中揭示的值也可能不同。
但除非有特殊事件提升了页面可访问的标识信息(本规范未定义此类事件),否则它们应当揭示相同的值。
关于隐私考虑,请参见 § 2.2.6 适配器标识符。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUAdapterInfo {readonly attribute DOMString vendor ;readonly attribute DOMString architecture ;readonly attribute DOMString device ;readonly attribute DOMString description ;readonly attribute unsigned long subgroupMinSize ;readonly attribute unsigned long subgroupMaxSize ;readonly attribute boolean isFallbackAdapter ; };
GPUAdapterInfo
拥有如下属性:
-
vendor, 类型为 DOMString,只读 -
适配器的厂商名称(如有)。否则为空字符串。
-
architecture, 类型为 DOMString,只读 -
适配器所属 GPU 家族或类别的名称(如有)。否则为空字符串。
-
device, 类型为 DOMString,只读 -
适配器的厂商自定义标识符(如有)。否则为空字符串。
注意: 这是代表适配器类型的值。例如,可能是 PCI 设备ID。它不会像序列号一样唯一标识某块硬件。
-
description, 类型为 DOMString,只读 -
驱动报告的关于适配器的人类可读字符串描述(如有)。否则为空字符串。
注意:
description没有采用任何格式化,不建议尝试解析此值。根据GPUAdapterInfo改变行为的应用(如为已知驱动问题应用修正),应尽量依赖其他字段。 -
subgroupMinSize, 类型为 unsigned long,只读 -
如果支持
"subgroups"特性,则为适配器支持的最小 子组大小。 -
subgroupMaxSize, 类型为 unsigned long,只读 -
如果支持
"subgroups"特性,则为适配器支持的最大 子组大小。 -
isFallbackAdapter, 类型为 boolean,只读 -
适配器是否为回退适配器。
-
令 adapterInfo 为新的
GPUAdapterInfo。 -
如果已知厂商,则将 adapterInfo.
vendor设为 adapter 的厂商名,并格式化为规范化标识符字符串。为保护隐私,用户代理也可以将 adapterInfo.vendor设为空字符串,或一个合理近似的厂商名(同样为规范化标识符字符串)。 -
如果已知架构,则将 adapterInfo.
architecture设为代表 adapter 所属 GPU 家族或类别的规范化标识符字符串。为保护隐私,用户代理也可以设为空字符串,或合理近似的架构名(同样为规范化标识符字符串)。 -
如果已知设备,则将 adapterInfo.
device设为 adapter 的厂商自定义标识符,并格式化为规范化标识符字符串。为保护隐私,用户代理也可以设为空字符串,或合理近似的标识符(同样为规范化标识符字符串)。 -
如果已知描述,则将 adapterInfo.
description设为驱动报告的 adapter 描述。为保护隐私,用户代理也可以设为空字符串,或合理近似的描述。 -
如果支持
"subgroups"特性,则将subgroupMinSize设为支持的最小子组大小;否则设为 4。注意: 为保护隐私,用户代理可以选择不支持某些特性,或为属性提供不会区分不同设备但依然可用的值(如对所有设备使用默认值 4)。
-
如果支持
"subgroups"特性,则将subgroupMaxSize设为支持的最大子组大小;否则设为 128。注意: 为保护隐私,用户代理可以选择不支持某些特性,或为属性提供不会区分不同设备但依然可用的值(如对所有设备使用默认值 128)。
-
将 adapterInfo.
isFallbackAdapter设为 adapter.[[fallback]]。 -
返回 adapterInfo。
[a-z0-9]+(-[a-z0-9]+)*
3.7. 扩展文档
“扩展文档”是描述新功能的附加文档,这些功能是非规范性的,不属于 WebGPU/WGSL 规范的一部分。
它们描述了在这些规范基础上构建的功能,通常包含一个或多个新的 API 特性标志和/或
WGSL enable 指令,或与其他草案 Web 规范的交互。
WebGPU 实现不得暴露扩展功能;这样做属于规范违规。 新功能只有在被集成到 WebGPU 规范(本文档)和/或 WGSL 规范后,才成为 WebGPU 标准的一部分。
3.8. 源限制(Origin Restrictions)
WebGPU 允许访问存储在图片、视频和画布中的图像数据。 出于安全原因,对跨域媒体的使用施加了限制,因为着色器可以被用来间接推测已上传到 GPU 的纹理内容。
WebGPU 不允许上传 不是 origin-clean 的图片源。
这也意味着,使用 WebGPU 渲染的 canvas 的 origin-clean 标志永远不会被置为 false。
关于为图片和视频元素发起 CORS 请求的更多信息,请参考:
3.9. 任务源(Task Sources)
3.9.1. WebGPU 任务源
WebGPU 定义了一个新的 任务源,称为 WebGPU 任务源。
它用于 uncapturederror
事件和
GPUDevice.lost。
GPUDevice
device 排队一个全局任务(queue a global task),并在 内容时序 上执行一系列步骤
steps:
-
在 WebGPU 任务源 上,为用于创建 device 的全局对象,按 steps 排队一个全局任务。
3.9.2. 自动过期任务源
WebGPU 定义了一个新的 任务源,称为 自动过期任务源。 它用于自动、定时地销毁某些对象:
来自 自动过期任务源 的任务应当以高优先级处理;尤其是排入队列后,应当在用户自定义(JavaScript)任务之前执行。
实现说明: 以高优先级处理过期“任务”也可以通过在事件循环处理模型的某个固定点插入额外步骤来实现,而不一定非要运行实际的任务。
3.10. 色彩空间与编码
WebGPU 不提供色彩管理。WebGPU 内的所有值(如纹理元素)都是原始数值,不是经过色彩管理的色值。
WebGPU 确实与色彩管理的输出(通过 GPUCanvasConfiguration)和输入(通过
copyExternalImageToTexture()
及 importExternalTexture())对接。因此,必须在
WebGPU 数值和外部色值之间进行色彩转换。每个接口点都会本地定义一种编码(色彩空间、传递函数和 alpha 预乘),用于解释 WebGPU 的数值。
WebGPU 允许 PredefinedColorSpace
枚举中的所有色彩空间。注意,每个色彩空间都定义了扩展范围(详见 CSS 相关定义),可以表示其空间外的颜色值(包括色度和明度)。
超出色域的预乘
RGBA 值指 R/G/B 其中任意一个通道的值大于 alpha 通道的值。例如,预乘 sRGB RGBA 值 [1.0, 0, 0, 0.5] 表示原始(未预乘)颜色 [2, 0, 0] 且
alpha 为 50%,在 CSS 中可写作 rgb(srgb 2 0 0 / 50%)。和所有 sRGB 色域外的颜色一样,这在扩展色彩空间中是有定义的点(alpha 为 0
时除外,此时没有颜色)。但若此类值输出到可见画布,结果未定义(见 GPUCanvasAlphaMode
"premultiplied")。
3.10.1. 色彩空间转换
颜色在空间间转换时,需根据上述定义将其在一个空间中的表示转换为另一个空间中的表示。
如果源值少于4个 RGBA 通道,则缺失的绿/蓝/alpha 通道分别设为0, 0, 1,然后再做色彩空间/编码和 alpha 预乘转换。转换后,如果目标需要的通道数少于4,则忽略多余通道。
注意: 灰度图像通常在其色彩空间下表现为 RGB 值 (V, V, V) 或 RGBA
值 (V, V, V, A)。
颜色在转换时不会被有损截断:若源色值本就在目标色彩空间的色域外,转换后会产生超出[0, 1]范围的值。例如,sRGB 目标下,如果源为 rgba16float,色彩空间更广(如 Display-P3),或为预乘且包含超出色域的预乘值,都可能出现上述情况。
同样,如果源值有高位深(如 16 位/分量 PNG)或扩展范围(如 float16 存储的 canvas),这些颜色会在转换过程中保留,且中间计算的精度不少于源。
3.10.2. 色彩空间转换省略
若源与目标的色彩空间/编码一致,则无需转换。通常,若某一步是恒等函数(无操作),实现应当出于性能考虑省略该步骤。
为获得最佳性能,应用应当设置色彩空间和编码选项,以最小化整个流程中所需的转换次数。
对于各种 GPUCopyExternalImageSourceInfo
图像源:
-
-
预乘由
premultiplyAlpha控制。 -
色彩空间由
colorSpaceConversion控制。
-
-
2d 画布:
-
色彩空间由
colorSpace创建上下文时的属性控制。
-
WebGL 画布:
-
预乘由
WebGLContextAttributes中的premultipliedAlpha选项控制。 -
色彩空间由
WebGLRenderingContextBase的drawingBufferColorSpace状态控制。
-
注意: 在依赖这些特性前,请检查浏览器的实现支持。
3.11. JavaScript 到 WGSL 的数值转换
WebGPU API 的多个部分(可覆盖管线 constants
和渲染通道清除值)会接收来自 WebIDL(double
或 float)的数值,并将其转换为
WGSL 值(bool、i32、u32、f32、f16)。
double
或 float
的 IDL 值 idlValue 转换为 WGSL 类型(to WGSL type) T,可能抛出 TypeError,请执行以下
设备时序 步骤:
注意: 该 TypeError
只会在 设备时序 内产生,并不会抛到
JavaScript。
-
断言 idlValue 是有限值,因为它不是
unrestricted double或unrestricted float。 -
令 v 为 将 idlValue 转换为 ECMAScript 值 后得到的 ECMAScript Number。
-
- 如果 T 是
bool -
返回与 ! 将 v 转换为 IDL 类型
boolean后结果对应的 WGSLbool值。注意: 本算法是在从 ECMAScript 值到 IDL
double或float值的转换之后调用的。如果原始 ECMAScript 值为非数值、非布尔值(如[]或{}),那么 WGSLbool结果可能与直接转换为 IDLboolean的结果不同。 - 如果 T 是
i32 -
返回 WGSL
i32值,该值对应于 ? 将 v 转换为类型为 [EnforceRange]long的 IDL 值 的结果。 - 如果 T 是
u32 -
返回 WGSL
u32值,该值对应于 ? 将 v 转换为类型为 [EnforceRange]unsigned long的 IDL 值 的结果。 - 如果 T 是
f32 - 如果 T 是
f16 -
-
令 wgslF32 为 WGSL
f32值,该值对应于 ? 将 v 转换为类型为float的 IDL 值 的结果。 -
返回
f16(wgslF32),即 ! 按 WGSL 浮点数转换 所定义,将 WGSLf32值转换为f16的结果。
注意:只要值在
f32的范围内,即使超出f16的范围,也不会抛出错误。 -
- 如果 T 是
GPUColor
color 转换为纹理格式的 texel 值(to a texel value of texture
format) format,可能抛出 TypeError,请执行以下
设备时序 步骤:
注意: 该 TypeError
只会在 设备时序 内产生,并不会抛到
JavaScript。
-
若 format 的各分量类型(断言全部相同)为:
- 浮点类型或归一化类型
-
令 T 为
f32。 - 有符号整型
-
令 T 为
i32。 - 无符号整型
-
令 T 为
u32。
-
令 wgslColor 为 WGSL
vec4<T>,其 4 个分量为 color 的 RGBA 通道,每个都 转换为 WGSL 类型 T。 -
按 § 23.2.7 输出合并 的同样规则将 wgslColor 转换为 format,并返回结果。
注意: 对于非整型类型,实际取值为实现定义。对于归一化类型,值会被限制在该类型允许的范围内。
注意: 换言之,写入的值就如同由 WGSL 着色器以
f32、i32 或 u32 类型的 vec4 输出。
4. 初始化
4.1. navigator.gpu
在 Window
和 WorkerGlobalScope
上下文中,GPU 对象通过 Navigator
和 WorkerNavigator
接口暴露,访问方式为 navigator.gpu:
interface mixin { [NavigatorGPU SameObject ,SecureContext ]readonly attribute GPU gpu ; };Navigator includes NavigatorGPU ;WorkerNavigator includes NavigatorGPU ;
NavigatorGPU
具有以下属性:
gpu,类型为 GPU,只读-
一个全局单例,提供诸如
requestAdapter()的顶级入口点。
4.2. GPU
GPU 是
WebGPU 的入口点。
[Exposed =(Window ,Worker ),SecureContext ]interface GPU {Promise <GPUAdapter ?>requestAdapter (optional GPURequestAdapterOptions options = {});GPUTextureFormat getPreferredCanvasFormat (); [SameObject ]readonly attribute WGSLLanguageFeatures wgslLanguageFeatures ; };
GPU 具有以下方法:
requestAdapter(options)-
从用户代理请求一个 adapter。用户代理选择是否返回适配器,并根据提供的选项进行选择。
调用对象:GPUthis.参数:
参数表:GPU.requestAdapter(options) 方法。 参数 类型 可空 可选 描述 optionsGPURequestAdapterOptions✘ ✔ 用于选择适配器的条件。 返回:
Promise<GPUAdapter?>内容时序步骤:
-
令 contentTimeline 为当前 内容时序。
-
令 promise 为 一个新的 promise。
-
在 this 的 设备时序上执行 初始化步骤。
-
返回 promise。
设备时序 初始化步骤:-
以下步骤的所有要求必须满足。
-
options.
featureLevel必须为一个特性级别字符串。
如果满足要求且用户代理选择返回一个适配器:
-
设置 adapter 为根据 § 4.2.2 适配器选择 中的规则和 options 的条件选择的 adapter,并遵循 § 4.2.1 适配器能力保证。根据定义初始化适配器的属性:
-
根据适配器的支持能力设置 adapter.
[[limits]]和 adapter.[[features]]。adapter.[[features]]必须包含"core-features-and-limits"。 -
如果 adapter 满足回退适配器的条件,则将 adapter.
[[fallback]]设置为true。否则,设置为false。 -
将 adapter.
[[xrCompatible]]设置为 options.xrCompatible。
-
否则:
-
令 adapter 为
null。
-
-
在 contentTimeline 上执行后续步骤。
内容时序步骤:-
如果 adapter 不为
null:-
解析(Resolve) promise,返回一个封装了 adapter 的新
GPUAdapter。
-
-
否则,解析(Resolve) promise,返回
null。
-
-
getPreferredCanvasFormat() -
返回在本系统上显示 8 位深度、标准动态范围内容的最佳
GPUTextureFormat。只能返回"rgba8unorm"或"bgra8unorm"。返回值可作为
format传递给configure()方法,以确保相关GPUCanvasContext能高效显示其内容。注意: 未显示到屏幕上的 canvas 使用此格式可能无益,也可能有益。
调用对象:GPUthis。返回:
GPUTextureFormat内容时序 步骤:
-
根据本系统 WebGPU canvas 显示的最佳格式,返回
"rgba8unorm"或"bgra8unorm"。
-
GPU 有以下属性:
-
wgslLanguageFeatures, 类型为 WGSLLanguageFeatures,只读 -
支持的 WGSL 语言扩展的名称。 支持的语言扩展会自动启用。
适配器 可能 随时被 过期。
当系统状态发生任何可能影响任何 requestAdapter()
调用结果的变化时,用户代理 应当 使所有 先前返回的 适配器 过期。例如:
-
物理适配器被添加/移除(通过插拔、驱动更新、死机恢复等)
-
系统电源配置发生变化(如笔记本电脑断电、电源设置更改等)
注意:
用户代理可能会选择经常 过期 适配器,即使系统状态没有发生变化(例如在适配器创建后几秒或几分钟)。
这有助于混淆真实的系统状态变化,并让开发者更加意识到,在调用 requestAdapter()
之前,总是需要重新请求适配器再调用 requestDevice()。
如果应用遇到这种情况,标准的设备丢失恢复处理应当能够使其恢复。
4.2.1. 适配器能力保证
任何由 GPUAdapter
通过 requestAdapter()
返回的对象都必须提供以下保证:
-
以下至少一项必须为真:
-
如果支持
"texture-compression-bc-sliced-3d",则必须支持"texture-compression-bc"。 -
如果支持
"texture-compression-astc-sliced-3d",则必须支持"texture-compression-astc"。 -
所有alignment-class 类型的限制值必须为2的幂。
-
maxBindingsPerBindGroup必须大于等于 (每个着色器阶段的最大绑定数 × 每个管线的最大着色器阶段数),其中:-
每个着色器阶段的最大绑定数为 (
maxSampledTexturesPerShaderStage+maxSamplersPerShaderStage+maxStorageBuffersPerShaderStage+maxStorageTexturesPerShaderStage+maxUniformBuffersPerShaderStage). -
每个管线的最大着色器阶段数为
2,因为GPURenderPipeline同时支持顶点和片元着色器。
注意:
maxBindingsPerBindGroup并不反映一个基本限制; 实现应通过提高该值来满足这一要求,而不是降低其他限制。 -
-
minUniformBufferOffsetAlignment和minStorageBufferOffsetAlignment都必须大于等于 32 字节。注意: 32 字节是
vec4<f64>的对齐值。参见 WebGPU 着色语言 § 14.4.1 对齐与大小。 -
maxStorageBufferBindingSize必须为4字节的整数倍。 -
maxVertexBufferArrayStride必须为4字节的整数倍。 -
maxComputeWorkgroupSizeX必须小于等于maxComputeInvocationsPerWorkgroup。 -
maxComputeWorkgroupSizeY必须小于等于maxComputeInvocationsPerWorkgroup。 -
maxComputeWorkgroupSizeZ必须小于等于maxComputeInvocationsPerWorkgroup。 -
maxComputeInvocationsPerWorkgroup必须小于等于maxComputeWorkgroupSizeX×maxComputeWorkgroupSizeY×maxComputeWorkgroupSizeZ。
4.2.2. 适配器选择
GPURequestAdapterOptions
为用户代理提供指示,表明哪些配置适合应用程序。
dictionary GPURequestAdapterOptions {DOMString featureLevel = "core";GPUPowerPreference powerPreference ;boolean forceFallbackAdapter =false ;boolean xrCompatible =false ; };
enum {GPUPowerPreference "low-power" ,"high-performance" , };
GPURequestAdapterOptions
拥有如下成员:
featureLevel, 类型为 DOMString,默认值为"core"-
适配器请求的“功能级别”。
允许的 功能级别字符串值为:
- "core"
-
无影响。
- "compatibility"
-
无影响。
注意: 该值为将来引入额外验证限制时保留。 目前应用不应使用此值。
powerPreference, 类型为 GPUPowerPreference-
可选,为用户代理提供一个提示,指示应从系统可用适配器中选择哪种类别的 适配器。
该提示的值可能影响选择哪个适配器,但不得影响是否返回适配器。
注意: 该提示的主要用途是在多GPU系统上影响所用的GPU。 例如,某些笔记本电脑有低功耗集成GPU和高性能独立GPU。该提示也可能影响所选GPU的电源配置,以匹配请求的电源偏好。
注意: 根据具体硬件配置(如电池状态、外接显示器或可卸载GPU),即使设置了相同的电源偏好,用户代理可能依然选择不同的 适配器。 通常,在相同硬件配置和状态下,且
powerPreference一致,用户代理通常会选择同一个适配器。必须为以下值之一:
undefined(或未设置)-
不给用户代理提供任何提示。
"low-power"-
表示请求优先考虑节能而非性能。
注意: 如果内容渲染需求较低(如每秒只渲染一帧,仅绘制相对简单的几何体和着色器,或只用小型HTML canvas元素),一般应使用该选项。 如果内容允许,鼓励开发者使用此值,因为这可能大幅提升便携设备的电池寿命。
"high-performance"-
表示请求优先考虑性能而非能耗。
注意: 选择此值时,开发者应注意,对于在所选适配器上创建的 设备,用户代理更可能强制设备丢失,以便切换到低功耗适配器节能。 只有在确有必要时,开发者才应指定此值,因为它可能会大幅降低便携设备的电池寿命。
forceFallbackAdapter, 类型为 boolean,默认值为false-
若设为
true,表示只能返回 回退适配器。如果用户代理不支持 回退适配器,则requestAdapter()会返回null。注意: 即使
forceFallbackAdapter设为false,requestAdapter()仍可能返回 回退适配器(当没有其他合适的 适配器可用,或用户代理选择返回回退适配器时)。 希望避免应用在 回退适配器上运行的开发者应在请求GPUDevice前检查info.isFallbackAdapter属性。 xrCompatible, 类型为 boolean,默认值为false-
若设为
true,表示必须返回用于 WebXR会话渲染的最佳 适配器。如果用户代理或系统不支持 WebXR会话,则适配器选择可忽略此值。注意: 如果在请求适配器时
xrCompatible未设为true,则从该适配器创建的GPUDevice无法用于 WebXR会话 渲染。
"high-performance"
GPUAdapter:
const gpuAdapter= await navigator. gpu. requestAdapter({ powerPreference: 'high-performance' });
4.3. GPUAdapter
GPUAdapter
封装了一个 适配器,
并描述其能力(特性
和 限制)。
要获取一个 GPUAdapter,
请使用 requestAdapter()。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUAdapter { [SameObject ]readonly attribute GPUSupportedFeatures features ; [SameObject ]readonly attribute GPUSupportedLimits limits ; [SameObject ]readonly attribute GPUAdapterInfo info ;Promise <GPUDevice >requestDevice (optional GPUDeviceDescriptor descriptor = {}); };
GPUAdapter
拥有以下不可变属性
features, 类型为 GPUSupportedFeatures,只读-
在
this.[[adapter]].[[features]]中的值集合。 limits, 类型为 GPUSupportedLimits,只读-
在
this.[[adapter]].[[limits]]中的限制。 info, 类型为 GPUAdapterInfo,只读-
关于该
GPUAdapter所对应物理适配器的信息。对于同一个
GPUAdapter, 暴露的GPUAdapterInfo值在时间上是恒定的。每次返回的都是同一个对象。首次创建该对象的方法如下:
[[adapter]],类型为 adapter,只读-
该
GPUAdapter所引用的 适配器。
GPUAdapter
拥有以下方法:
requestDevice(descriptor)-
这是一次性操作:如果成功返回了设备,则适配器变为
"consumed"。调用对象:GPUAdapterthis.参数:
方法 GPUAdapter.requestDevice(descriptor) 的参数。 参数 类型 可为 null 可选 描述 descriptorGPUDeviceDescriptor✘ ✔ 请求的 GPUDevice的描述信息。内容时间线 步骤:
-
令 contentTimeline 为当前 内容时间线。
-
令 promise 为新的 promise。
-
令 adapter 为 this.
[[adapter]]。 -
对 this 的 设备时间线 执行 初始化步骤。
-
返回 promise。
设备时间线 初始化步骤:-
如果下列任何要求未满足:
-
descriptor.
requiredFeatures中的值集合 必须是 adapter.[[features]]的子集。
则在 contentTimeline 上执行下列步骤并返回:
注意: 这与浏览器完全不识别某特性的情况下抛出的错误相同(在其
GPUFeatureName定义里)。 这使得浏览器不支持某特性和某适配器不支持某特性时的行为一致。 -
-
以下步骤中的所有要求 必须 被满足。
-
adapter.
[[state]]不得为"consumed"。 -
对于 descriptor.
requiredLimits中每个 key、value 对(且 value 不为undefined):-
key 必须 是 支持的限制的成员名。
-
value 不得比 adapter.
[[limits]][key] 更高。
注意: 即使 value 为
undefined,当 key 未被识别时,用户代理应考虑给出开发者可见的警告。 -
若有未满足项,则在 contentTimeline 上执行下列步骤并返回:
内容时间线 步骤:-
拒绝 promise,并给出
OperationError。
-
-
如果 adapter.
[[state]]为"expired"或用户代理无法完成该请求:-
令 device 为新的 设备。
-
断言 adapter.
[[state]]为"expired"。注意: 当发生这种情况时,用户代理应考虑在大多数或所有情况下向开发者发出警告。应用应从
requestAdapter()开始重新初始化逻辑。
否则:
-
令 device 为带有 descriptor 描述能力的新设备。
-
-
在 contentTimeline 上执行后续步骤。
内容时间线 步骤:-
令 gpuDevice 为新的
GPUDevice实例。 -
设置 gpuDevice.
[[device]]为 device。 -
设置 device.
[[content device]]为 gpuDevice。 -
解决 promise,返回 gpuDevice。
注意: 如果由于适配器无法完成请求而设备已丢失,则 device.
lost在 promise 解决前已经解决。
-
GPUDevice:
const gpuAdapter= await navigator. gpu. requestAdapter(); const gpuDevice= await gpuAdapter. requestDevice();
4.3.1. GPUDeviceDescriptor
GPUDeviceDescriptor
描述一个设备请求。
dictionary GPUDeviceDescriptor :GPUObjectDescriptorBase {sequence <GPUFeatureName >requiredFeatures = [];record <DOMString , (GPUSize64 or undefined )>requiredLimits = {};GPUQueueDescriptor defaultQueue = {}; };
GPUDeviceDescriptor
拥有以下成员:
requiredFeatures, 类型为 sequence<GPUFeatureName>,默认值为[]-
指定设备请求所需的特性。 如果适配器无法提供这些特性,请求会失败。
API调用的校验只允许恰好指定的特性,不多也不少。
requiredLimits, 类型为record<DOMString, (GPUSize64 or undefined)>,默认值为{}-
指定设备请求所需的限制。 如果适配器无法提供这些限制,请求会失败。
每个非
undefined值的 key 必须是支持的限制的成员名。在最终设备上的 API 调用会根据该设备的实际限制(不是适配器的限制;见 § 3.6.2 限制)进行校验。
defaultQueue, 类型为 GPUQueueDescriptor,默认值为{}-
默认
GPUQueue的描述符。
"texture-compression-astc"
特性时,请求 GPUDevice:
const gpuAdapter= await navigator. gpu. requestAdapter(); const requiredFeatures= []; if ( gpuAdapter. features. has( 'texture-compression-astc' )) { requiredFeatures. push( 'texture-compression-astc' ) } const gpuDevice= await gpuAdapter. requestDevice({ requiredFeatures});
GPUDevice
并指定更高的 maxColorAttachmentBytesPerSample
限制:
const gpuAdapter= await navigator. gpu. requestAdapter(); if ( gpuAdapter. limits. maxColorAttachmentBytesPerSample< 64 ) { // 当所需限制不被支持时,采取措施,要么降级代码路径,要么提示用户其设备不满足最低要求。 } // 请求更高的每样本最大颜色附件字节数限制。 const gpuDevice= await gpuAdapter. requestDevice({ requiredLimits: { maxColorAttachmentBytesPerSample: 64 }, });
4.3.1.1. GPUFeatureName
每个 GPUFeatureName
都标识一组功能,如果可用,可使 WebGPU 支持本来无效的额外用法。
enum GPUFeatureName {"core-features-and-limits" ,"depth-clip-control" ,"depth32float-stencil8" ,"texture-compression-bc" ,"texture-compression-bc-sliced-3d" ,"texture-compression-etc2" ,"texture-compression-astc" ,"texture-compression-astc-sliced-3d" ,"timestamp-query" ,"indirect-first-instance" ,"shader-f16" ,"rg11b10ufloat-renderable" ,"bgra8unorm-storage" ,"float32-filterable" ,"float32-blendable" ,"clip-distances" ,"dual-source-blending" ,"subgroups" ,"texture-formats-tier1" ,"texture-formats-tier2" , };
4.4. GPUDevice
要获取一个 GPUDevice,请使用
requestDevice()。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUDevice :EventTarget { [SameObject ]readonly attribute GPUSupportedFeatures features ; [SameObject ]readonly attribute GPUSupportedLimits limits ; [SameObject ]readonly attribute GPUAdapterInfo adapterInfo ; [SameObject ]readonly attribute GPUQueue queue ;undefined destroy ();GPUBuffer createBuffer (GPUBufferDescriptor descriptor );GPUTexture createTexture (GPUTextureDescriptor descriptor );GPUSampler createSampler (optional GPUSamplerDescriptor descriptor = {});GPUExternalTexture importExternalTexture (GPUExternalTextureDescriptor descriptor );GPUBindGroupLayout createBindGroupLayout (GPUBindGroupLayoutDescriptor descriptor );GPUPipelineLayout createPipelineLayout (GPUPipelineLayoutDescriptor descriptor );GPUBindGroup createBindGroup (GPUBindGroupDescriptor descriptor );GPUShaderModule createShaderModule (GPUShaderModuleDescriptor descriptor );GPUComputePipeline createComputePipeline (GPUComputePipelineDescriptor descriptor );GPURenderPipeline createRenderPipeline (GPURenderPipelineDescriptor descriptor );Promise <GPUComputePipeline >createComputePipelineAsync (GPUComputePipelineDescriptor descriptor );Promise <GPURenderPipeline >createRenderPipelineAsync (GPURenderPipelineDescriptor descriptor );GPUCommandEncoder createCommandEncoder (optional GPUCommandEncoderDescriptor descriptor = {});GPURenderBundleEncoder createRenderBundleEncoder (GPURenderBundleEncoderDescriptor descriptor );GPUQuerySet createQuerySet (GPUQuerySetDescriptor descriptor ); };GPUDevice includes GPUObjectBase ;
features, 类型为 GPUSupportedFeatures,只读-
包含设备支持的
GPUFeatureName的集合([[device]].[[features]])。 limits, 类型为 GPUSupportedLimits,只读-
该设备支持的限制(
[[device]].[[limits]])。 queue, 类型为 GPUQueue,只读-
此设备的主
GPUQueue。 adapterInfo, 类型为 GPUAdapterInfo,只读-
创建此 设备的物理适配器的信息,该
GPUDevice所指向的适配器。对于同一个
GPUDevice,暴露的GPUAdapterInfo值在时间上是恒定的。每次返回的都是同一个对象。首次创建该对象的方法如下:
[[device]]
是 GPUDevice
所引用的 设备。
GPUDevice
拥有以下方法:
destroy()-
销毁该 设备,阻止进一步的操作。所有未完成的异步操作将失败。
注意: 多次销毁同一个设备是合法的。
-
丢失该设备(this.
[[device]],"destroyed")。
注意: 由于无法在该设备上排队新的操作,实现可以立即中止所有未完成的异步操作并释放资源,包括刚刚解除映射的内存。
-
GPUDevice 的
允许的缓冲区用法 包括:
GPUDevice 的
允许的纹理用法 包括:
4.5. 示例
GPUAdapter
和 GPUDevice
并进行错误处理的示例:
let gpuDevice= null ; async function initializeWebGPU() { // 检查用户代理是否支持 WebGPU。 if ( ! ( 'gpu' in navigator)) { console. error( "用户代理不支持 WebGPU。" ); return false ; } // 请求适配器。 const gpuAdapter= await navigator. gpu. requestAdapter(); // 如果找不到合适的适配器,requestAdapter 可能会 resolve 为 null。 if ( ! gpuAdapter) { console. error( '未找到 WebGPU 适配器。' ); return false ; } // 请求设备。 // 注意:如果给可选字典传递了无效参数,promise 会 reject。为避免 promise 被拒绝, // 总是应在调用 requestDevice() 前检查特性和限制是否被适配器支持。 gpuDevice= await gpuAdapter. requestDevice(); // requestDevice 永远不会返回 null,但如果出于某种原因无法满足有效的设备请求, // 它可能会返回一个已经丢失的设备。 // 此外,设备在创建后可能随时丢失(如:浏览器资源管理、驱动更新), // 因此应始终优雅地处理设备丢失。 gpuDevice. lost. then(( info) => { console. error( `WebGPU 设备已丢失: ${ info. message} ` ); gpuDevice= null ; // 多数设备丢失的原因是临时性的,因此应用应在丢失后尝试获取新设备, // 除非丢失原因是应用主动销毁了设备。注意,所有用旧设备创建的 WebGPU 资源(缓冲区、纹理等) // 都需要用新设备重新创建。 if ( info. reason!= 'destroyed' ) { initializeWebGPU(); } }); onWebGPUInitialized(); return true ; } function onWebGPUInitialized() { // 在这里创建 WebGPU 资源…… } initializeWebGPU();
5. 缓冲区
5.1. GPUBuffer
GPUBuffer
表示一块可用于 GPU 操作的内存块。
数据以线性布局存储,这意味着分配的每个字节都可以通过其相对于 GPUBuffer
起始位置的偏移来寻址,
但具体操作可能有对齐限制。某些 GPUBuffers
可以被映射,使这块内存可以通过一个称为映射的 ArrayBuffer
访问。
GPUBuffer
通过 createBuffer()
创建。
缓冲区可以 mappedAtCreation。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUBuffer {readonly attribute GPUSize64Out size ;readonly attribute GPUFlagsConstant usage ;readonly attribute GPUBufferMapState mapState ;Promise <undefined >mapAsync (GPUMapModeFlags mode ,optional GPUSize64 offset = 0,optional GPUSize64 size );ArrayBuffer getMappedRange (optional GPUSize64 offset = 0,optional GPUSize64 size );undefined unmap ();undefined destroy (); };GPUBuffer includes GPUObjectBase ;enum GPUBufferMapState {"unmapped" ,"pending" ,"mapped" , };
mapState, 类型为 GPUBufferMapState,只读-
缓冲区的当前
GPUBufferMapState:"unmapped"-
缓冲区未被
this.getMappedRange()映射使用。 "pending"-
缓冲区已请求映射,但尚未完成。映射可能成功,也可能在
mapAsync()校验时失败。 "mapped"-
缓冲区已映射,可以通过
this.getMappedRange()使用。
getter 步骤如下:
内容时间线 步骤:-
如果 this.
[[mapping]]不为null,返回"mapped"。 -
如果 this.
[[pending_map]]不为null,返回"pending"。 -
返回
"unmapped"。
[[pending_map]],类型为Promise<void> 或null,初始为null-
当前挂起的
mapAsync()调用返回的Promise。永远不会有多个挂起的映射,因为如果已有请求在进行中,
mapAsync()会立即拒绝。 [[mapping]],类型为 活动缓冲区映射 或null,初始为null-
仅当缓冲区当前被
getMappedRange()映射使用时才设置。否则为 null(即使有[[pending_map]])。活动缓冲区映射 是具有以下字段的结构:
- data,类型为 数据块
-
本
GPUBuffer的映射数据。应用通过ArrayBuffer视图访问,由getMappedRange()返回并存储在 views 中。 - mode,类型为
GPUMapModeFlags -
映射的
GPUMapModeFlags,由相应mapAsync()或createBuffer()调用指定。 - range,类型为元组 [
unsigned long long,unsigned long long] -
该
GPUBuffer被映射的范围。 - views,类型为 list<
ArrayBuffer> -
通过
getMappedRange()返回给应用的ArrayBuffer。这些视图会在unmap()被调用时被追踪并分离。
要用 mode mode 和 range range 初始化活动缓冲区映射,执行以下 内容时间线 步骤:-
令 size 为 range[1] - range[0]。
-
令 data 为 ? CreateByteDataBlock(size)。
注意:这可能抛出RangeError。 为了保证一致和可预测性:-
对于
new ArrayBuffer()在某一时刻能成功的任意 size,此分配 也应 同时成功。 -
对于
new ArrayBuffer()确定性 抛出RangeError的任意 size,此分配 也应 抛出。
-
-
返回具有如下内容的 活动缓冲区映射:
[[internal state]]-
缓冲区的当前内部状态:
5.1.1. GPUBufferDescriptor
dictionary GPUBufferDescriptor :GPUObjectDescriptorBase {required GPUSize64 size ;required GPUBufferUsageFlags usage ;boolean mappedAtCreation =false ; };
GPUBufferDescriptor
拥有以下成员:
size, 类型为 GPUSize64-
缓冲区的字节大小。
usage, 类型为 GPUBufferUsageFlags-
缓冲区允许的用法。
mappedAtCreation, 类型为 boolean,默认值为false-
如果为
true,则缓冲区会以已映射状态创建,允许立即调用getMappedRange()。 即使usage不包含MAP_READ或MAP_WRITE,也可以设置mappedAtCreation为true。 这可用于设置缓冲区的初始数据。即使最终缓冲区创建失败,也保证看起来映射范围可读写直到其被解除映射。
5.1.2. 缓冲区用法
typedef [EnforceRange ]unsigned long ; [GPUBufferUsageFlags Exposed =(Window ,Worker ),SecureContext ]namespace {GPUBufferUsage const GPUFlagsConstant MAP_READ = 0x0001;const GPUFlagsConstant MAP_WRITE = 0x0002;const GPUFlagsConstant COPY_SRC = 0x0004;const GPUFlagsConstant COPY_DST = 0x0008;const GPUFlagsConstant INDEX = 0x0010;const GPUFlagsConstant VERTEX = 0x0020;const GPUFlagsConstant UNIFORM = 0x0040;const GPUFlagsConstant STORAGE = 0x0080;const GPUFlagsConstant INDIRECT = 0x0100;const GPUFlagsConstant QUERY_RESOLVE = 0x0200; };
GPUBufferUsage
标志决定了 GPUBuffer
创建后可以如何使用:
MAP_READ-
缓冲区可被映射用于读取。(例如:调用
mapAsync()并传入GPUMapMode.READ)只能与
COPY_DST组合使用。 MAP_WRITE-
缓冲区可被映射用于写入。(例如:调用
mapAsync()并传入GPUMapMode.WRITE)只能与
COPY_SRC组合使用。 COPY_SRC-
缓冲区可作为拷贝操作的源。(例如:作为 copyBufferToBuffer() 或
copyBufferToTexture()调用的source参数) COPY_DST-
缓冲区可作为拷贝或写入操作的目标。(例如:作为 copyBufferToBuffer() 或
copyTextureToBuffer()调用的destination参数,或作为writeBuffer()的目标。) INDEX-
缓冲区可作为索引缓冲区使用。(例如:传递给
setIndexBuffer()。) VERTEX-
缓冲区可作为顶点缓冲区使用。(例如:传递给
setVertexBuffer()。) UNIFORM-
缓冲区可用作 uniform 缓冲区。(例如:用于
GPUBufferBindingLayout的绑定组条目,且buffer.type为"uniform"。) STORAGE-
缓冲区可用作 storage 缓冲区。(例如:用于
GPUBufferBindingLayout的绑定组条目,且buffer.type为"storage"或"read-only-storage"。) INDIRECT-
缓冲区可用于存储间接命令参数。(例如:作为
indirectBuffer参数传递给drawIndirect()或dispatchWorkgroupsIndirect()调用。) QUERY_RESOLVE-
缓冲区可用于捕获查询结果。(例如:作为
destination参数传递给resolveQuerySet()。)
5.1.3. 缓冲区创建
createBuffer(descriptor)-
创建一个
GPUBuffer。调用对象:GPUDevicethis。参数:
GPUDevice.createBuffer(descriptor) 方法的参数。 参数 类型 可为 null 可选 描述 descriptorGPUBufferDescriptor✘ ✘ 要创建的 GPUBuffer描述信息。返回:
GPUBuffer内容时间线 步骤:
-
令 b 为 ! 创建新的 WebGPU 对象(this,
GPUBuffer, descriptor)。 -
如果 descriptor.
mappedAtCreation为true:-
如果 descriptor.
size不是 4 的倍数,抛出RangeError。 -
设置 b.
[[mapping]]为 ? 初始化活动缓冲区映射,mode 为WRITE,range 为[0, descriptor.。size]
-
-
在 this 的 设备时间线 上执行 初始化步骤。
-
返回 b。
设备时间线 初始化步骤:注意: 如果缓冲区创建失败,且 descriptor.
mappedAtCreation为false, 任何对mapAsync()的调用都会被拒绝,因此为支持映射而分配的任何资源都可以被回收。-
如果 descriptor.
mappedAtCreation为true:-
设置 b.
[[internal state]]为 "unavailable"。
否则:
-
设置 b.
[[internal state]]为 "available"。
-
-
为 b 创建设备分配,并将每个字节置零。
-
const buffer= gpuDevice. createBuffer({ size: 128 , usage: GPUBufferUsage. UNIFORM| GPUBufferUsage. COPY_DST});
5.1.4. 缓冲区销毁
应用如果不再需要某个 GPUBuffer,可以在垃圾回收前调用
destroy()
主动放弃访问它。
销毁缓冲区还会解除映射,释放为映射分配的内存。
注意: 这样允许用户代理在所有先前提交的与该 GPUBuffer
相关的操作完成后及时回收其 GPU 内存。
GPUBuffer
拥有以下方法:
destroy()-
销毁该
GPUBuffer。注意: 多次销毁同一个缓冲区是合法的。
设备时间线 步骤:-
设置 this.
[[internal state]]为 "destroyed"。
注意: 由于无法继续用该缓冲区排队新操作,实现可以立即释放资源分配,包括刚刚解除映射的内存。
-
5.2. 缓冲区映射
应用可以请求映射 GPUBuffer,从而通过代表该
GPUBuffer
分配部分的 ArrayBuffer
访问其内容。
映射 GPUBuffer
的请求是通过 mapAsync()
异步进行的,以便用户代理能确保 GPU 在应用访问内容前已完成对该 GPUBuffer 的使用。
被映射的 GPUBuffer 不能被
GPU 使用,必须通过 unmap()
解除映射后才能再次用于 队列时间线的工作提交。
一旦 GPUBuffer
被映射,应用可通过 getMappedRange()
同步访问其内容范围。
返回的 ArrayBuffer
只能被 unmap()(直接或通过
GPUBuffer.destroy()
或 GPUDevice.destroy())分离,不能被转移。
任何其它操作试图这样做都会抛出 TypeError。
typedef [EnforceRange ]unsigned long ; [GPUMapModeFlags Exposed =(Window ,Worker ),SecureContext ]namespace {GPUMapMode const GPUFlagsConstant READ = 0x0001;const GPUFlagsConstant WRITE = 0x0002; };
GPUMapMode
标志决定了调用 mapAsync()
时 GPUBuffer
被如何映射:
READ-
只对创建时带有
MAP_READ用法的缓冲区有效。映射后,调用
getMappedRange()将返回包含缓冲区当前值的ArrayBuffer。对返回的ArrayBuffer的更改在unmap()后会被丢弃。 WRITE-
只对创建时带有
MAP_WRITE用法的缓冲区有效。映射后,调用
getMappedRange()将返回包含缓冲区当前值的ArrayBuffer。对返回的ArrayBuffer的更改会在unmap()后写入缓冲区。注意: 由于
MAP_WRITE用法只能与COPY_SRC组合,写映射永远不会返回 GPU 产生的数据,返回的ArrayBuffer只包含初始化为零的数据或网页之前映射写入的数据。
GPUBuffer
拥有以下方法:
mapAsync(mode, offset, size)-
映射
GPUBuffer的指定范围,并在缓冲区内容可以通过getMappedRange()访问时 resolve 返回的Promise。返回的
Promise仅表示缓冲区已被映射,不保证其它 内容时间线可见操作完成,特别是不保证其它Promise(如onSubmittedWorkDone()或其它mapAsync())已 resolve。而
Promise由onSubmittedWorkDone()返回则保证在该队列上之前的所有mapAsync()已完成。调用对象:GPUBufferthis。参数:
GPUBuffer.mapAsync(mode, offset, size) 方法的参数。 参数 类型 可为 null 可选 描述 modeGPUMapModeFlags✘ ✘ 缓冲区应以读或写方式映射。 offsetGPUSize64✘ ✔ 要映射的范围起始字节偏移。 sizeGPUSize64✘ ✔ 要映射的范围字节大小。 内容时间线步骤:
-
令 contentTimeline 为当前 内容时间线。
-
若 this.
mapState不为"unmapped":-
在 this.
[[device]]的 设备时间线 上执行 early-reject steps。 -
返回 被拒绝的 promise,错误为
OperationError。
-
-
令 p 为新的
Promise。 -
设置 this.
[[pending_map]]= p。 -
在 this.
[[device]]的 设备时间线 上执行 validation steps。 -
返回 p。
设备时间线 validation steps:-
如果 size 为
undefined:-
令 rangeSize = max(0, this.
size- offset)。
否则:
-
令 rangeSize = size。
-
-
如果下述任一条件不满足:
-
this 必须是 有效的。
-
设置 deviceLost 为
true。 -
在 contentTimeline 上执行 map failure steps。
-
返回。
-
-
如果下述任一条件不满足:
否则:
-
设置 deviceLost 为
false。 -
在 contentTimeline 上执行 map failure steps。
-
返回。
-
-
将 this.
[[internal state]]设为 "unavailable"。注意: 缓冲区被映射后,在解除映射前其内容不会改变。
-
当下述任一事件发生(以先发生者为准),或已发生时:
-
-
在所有当前排队并使用 this 的操作完成后
-
且不晚于所有当前排队操作(不论是否使用 this)完成时
-
-
this.
[[device]]丢失。
然后在 this.
[[device]]的 设备时间线 上执行后续步骤。 -
设备时间线 步骤:-
若 this.
[[device]]已失效,则 deviceLost 设为true,否则为false。注意: 设备可能在前后步骤之间丢失。
-
如果 deviceLost:
-
在 contentTimeline 上执行 map failure steps。
否则:
-
令 internalStateAtCompletion = this.
[[internal state]]。注意: 只有当此时缓冲区因
unmap()变为 "available" 时,[[pending_map]]≠ p,后续 mapping 步骤不会成功。 -
令 dataForMappedRegion 为 this 从 offset 开始,长度为 rangeSize 字节的内容。
-
在 contentTimeline 上执行 map success steps。
-
内容时间线 map success steps:-
若 this.
[[pending_map]]≠ p:注意: 映射已被
unmap()取消。-
断言 p 已被拒绝。
-
返回。
-
-
断言 p 仍为 pending。
-
断言 internalStateAtCompletion 为 "unavailable"。
-
令 mapping = 初始化活动缓冲区映射,mode 为 mode,range 为
[offset, offset + rangeSize]。若分配失败:
-
设 this.
[[pending_map]]=null,并 拒绝 p,错误为RangeError。 -
返回。
-
-
将 mapping.data 设为 dataForMappedRegion。
-
设 this.
[[mapping]]= mapping。 -
设 this.
[[pending_map]]=null,并 resolve p。
内容时间线 map failure steps:-
如果 this.
[[pending_map]]≠ p:注意: 映射已被
unmap()取消。-
断言 p 已被拒绝。
-
返回。
-
-
断言 p 仍为 pending。
-
设 this.
[[pending_map]]=null。 -
如果 deviceLost:
-
拒绝 p 并抛出
AbortError。注意: 这和用
unmap()取消 map 时抛出的错误类型一致。
否则:
-
拒绝 p 并抛出
OperationError。
-
-
getMappedRange(offset, size)-
返回包含指定范围
GPUBuffer内容的ArrayBuffer。调用对象:GPUBufferthis。参数:
GPUBuffer.getMappedRange(offset, size) 方法的参数。 参数 类型 可为 null 可选 描述 offsetGPUSize64✘ ✔ 要返回内容的起始字节偏移。 sizeGPUSize64✘ ✔ 要返回的 ArrayBuffer字节长度。返回值:
ArrayBuffer内容时间线 步骤:
-
如果 size 未指定:
-
令 rangeSize = max(0, this.
size- offset)。
否则,rangeSize = size。
-
-
如以下任一条件不满足,则抛出
OperationError并返回:-
this.
[[mapping]]不为null。 -
offset 为 8 的倍数。
-
rangeSize 为 4 的倍数。
-
offset ≥ this.
[[mapping]].range[0]。 -
offset + rangeSize ≤ this.
[[mapping]].range[1]。 -
[offset, offset + rangeSize) 不与 this.
[[mapping]].views 中的其它范围重叠。
注意: 对
GPUBuffer,即使其为mappedAtCreation且 无效,只要 内容时间线 未知其无效,依然可以获取映射范围。 -
-
令 data = this.
[[mapping]].data。 -
令 view = 创建 ArrayBuffer,大小为 rangeSize,指针可变地引用 data 从 (offset -
[[mapping]].range[0]) 开始的内容。注意: 这里不会抛出
RangeError,因为 data 已在mapAsync()或createBuffer()时分配。 -
设置 view.
[[ArrayBufferDetachKey]]为 "WebGPUBufferMapping"。 -
追加 view 到 this.
[[mapping]].views。 -
返回 view。
注意: 若
getMappedRange()调用未先检查映射状态(如等待mapAsync()resolve、查询mapState为"mapped"或等待onSubmittedWorkDone()),用户代理应考虑发出开发警告。 -
unmap()-
解除
GPUBuffer的映射范围,使其内容重新可被 GPU 使用。调用对象:GPUBufferthis。返回值:
undefined内容时间线 步骤:
-
如果 this.
[[pending_map]]不为null:-
拒绝 this.
[[pending_map]],错误为AbortError。 -
设 this.
[[pending_map]]=null。
-
-
如果 this.
[[mapping]]为null:-
返回。
-
-
对于 this.
[[mapping]].views 中的每个ArrayBufferab:-
执行 DetachArrayBuffer(ab, "WebGPUBufferMapping")。
-
-
令 bufferUpdate =
null。 -
如果 this.
[[mapping]].mode 包含WRITE:-
设 bufferUpdate = {
data: this.[[mapping]].data,offset: this.[[mapping]].range[0] }。
注意: 如果缓冲区不是以
WRITE模式映射,解除映射时应用对映射范围ArrayBuffer的本地修改会被丢弃,不会影响后续映射内容。 -
-
设 this.
[[mapping]]=null。 -
在 this.
[[device]]的 设备时间线 上执行后续步骤。
设备时间线 步骤:-
如下条件任一不满足则返回:
-
this 可与 this.
[[device]]安全配合使用。
-
-
断言 this.
[[internal state]]为 "unavailable"。 -
如果 bufferUpdate 不为
null:-
在 this.
[[device]].queue的 队列时间线 上执行:队列时间线 步骤:-
用 bufferUpdate.
data更新 this 从 bufferUpdate.offset开始的内容。
-
-
-
将 this.
[[internal state]]设为 "available"。
-
6. 纹理与纹理视图
6.1. GPUTexture
纹理(texture)
由 1d、
2d
或
3d
的数据数组组成,每个元素可包含多个值用于表示如颜色等信息。纹理的读写方式取决于创建时使用的
GPUTextureUsage。
例如,纹理可被采样、读写于渲染与计算管线着色器,也可由渲染通道输出写入。纹理在 GPU 内部通常以多维访问优化的布局存储,而非线性存储。
一个 纹理 由一个或多个
纹理子资源(texture
subresource)
组成,每个子资源由 mipmap 级别 唯一标识,
对于 2d
纹理,还包括 数组层 和 aspect(分量)。
纹理子资源 是一种 子资源:每个子资源可在单一 用法范围 内有不同 内部用途。
每个 mipmap 级别 的子资源
在每个空间维度上的尺寸大约是上一级资源的一半
(见 mipmap 级别特定逻辑纹理范围)。
0 级子资源的尺寸等于纹理本身的尺寸,更小的级别通常用于存储同一图像的低分辨率版本。
GPUSampler
与 WGSL 提供了选择与插值 细节级别
的能力,可显式或自动进行。
"2d"
纹理可以是 数组层(array layer)
的集合。
每一层的子资源尺寸都相同。非 2d 纹理的所有子资源的数组层索引都为 0。
每个子资源有一个 aspect(分量)。
彩色纹理只有一个分量:color。
深度或模板格式
的纹理可能有多个分量:
depth 分量,
stencil 分量,或两者皆有,
可被特殊用法所使用,如 depthStencilAttachment
和 "depth"
绑定。
"3d"
纹理可以有多个 切片(slice),每个切片是该纹理在特定
z 值下的二维图像。
切片不是独立的子资源。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUTexture {GPUTextureView createView (optional GPUTextureViewDescriptor descriptor = {});undefined destroy ();readonly attribute GPUIntegerCoordinateOut width ;readonly attribute GPUIntegerCoordinateOut height ;readonly attribute GPUIntegerCoordinateOut depthOrArrayLayers ;readonly attribute GPUIntegerCoordinateOut mipLevelCount ;readonly attribute GPUSize32Out sampleCount ;readonly attribute GPUTextureDimension dimension ;readonly attribute GPUTextureFormat format ;readonly attribute GPUFlagsConstant usage ; };GPUTexture includes GPUObjectBase ;
GPUTexture
拥有如下不可变属性:
width,类型为 GPUIntegerCoordinateOut,只读-
该
GPUTexture的宽度。 height,类型为 GPUIntegerCoordinateOut,只读-
该
GPUTexture的高度。 depthOrArrayLayers,类型为 GPUIntegerCoordinateOut,只读-
该
GPUTexture的深度或层数。 mipLevelCount,类型为 GPUIntegerCoordinateOut,只读-
该
GPUTexture的 mip 级别数量。 sampleCount,类型为 GPUSize32Out,只读-
该
GPUTexture的采样数。 dimension,类型为 GPUTextureDimension,只读-
每个子资源的 texel 集的维度。
format,类型为 GPUTextureFormat,只读-
该
GPUTexture的格式。 usage,类型为 GPUFlagsConstant,只读-
该
GPUTexture允许的用途。 [[viewFormats]],类型为 sequence<GPUTextureFormat>-
为此
GPUTexture创建视图时可用作GPUTextureViewDescriptor.format的GPUTextureFormat集合。
GPUTexture
拥有如下设备时间线属性:
[[destroyed]],类型为boolean, 初始值为false-
如果纹理被销毁,它将无法再被用于任何操作,其底层内存可以被释放。
参数:
-
GPUExtent3DbaseSize -
GPUSize32mipLevel
返回: GPUExtent3DDict
设备时间线步骤:
-
令 extent 为新的
GPUExtent3DDict对象。 -
设置 extent.
depthOrArrayLayers为 1。 -
返回 extent。
逻辑 mip 级别特定纹理范围(logical miplevel-specific texture extent) 指的是某个 纹理 在特定 mipLevel 下的 texel 尺寸。 其计算过程如下:
参数:
-
GPUTextureDescriptordescriptor -
GPUSize32mipLevel
返回: GPUExtent3DDict
-
令 extent 为新的
GPUExtent3DDict对象。 -
如果 descriptor.
dimension为:"1d"-
-
设 extent.
height= 1 -
设 extent.
depthOrArrayLayers= 1
"2d"-
-
设 extent.
depthOrArrayLayers= descriptor.size.depthOrArrayLayers
"3d"-
-
设 extent.
depthOrArrayLayers= max(1, descriptor.size.depthOrArrayLayers ≫ mipLevel)
-
返回 extent。
物理 mip 级别特定纹理范围(physical miplevel-specific texture extent) 指的是某个 纹理 在特定 mipLevel 下的 texel 尺寸,并包含为形成完整texel block而可能补齐的额外填充。其计算过程如下:
参数:
-
GPUTextureDescriptordescriptor -
GPUSize32mipLevel
返回: GPUExtent3DDict
-
令 extent 为新的
GPUExtent3DDict对象。 -
令 logicalExtent = 逻辑 mip 级别特定纹理范围(descriptor, mipLevel)。
-
如果 descriptor.
dimension为:"1d"-
-
设 extent.
width为 logicalExtent.width 向上取整到 descriptor 的 texel block width 的倍数。 -
设 extent.
height= 1 -
设 extent.
depthOrArrayLayers= 1
-
"2d"-
-
设 extent.
width为 logicalExtent.width 向上取整到 descriptor 的 texel block width 的倍数。 -
设 extent.
height为 logicalExtent.height 向上取整到 descriptor 的 texel block height 的倍数。 -
设 extent.
depthOrArrayLayers= logicalExtent.depthOrArrayLayers
-
"3d"-
-
设 extent.
width为 logicalExtent.width 向上取整到 descriptor 的 texel block width 的倍数。 -
设 extent.
height为 logicalExtent.height 向上取整到 descriptor 的 texel block height 的倍数。 -
设 extent.
depthOrArrayLayers= logicalExtent.depthOrArrayLayers
-
-
返回 extent。
6.1.1. GPUTextureDescriptor
dictionary GPUTextureDescriptor :GPUObjectDescriptorBase {required GPUExtent3D size ;GPUIntegerCoordinate mipLevelCount = 1;GPUSize32 sampleCount = 1;GPUTextureDimension dimension = "2d";required GPUTextureFormat format ;required GPUTextureUsageFlags usage ;sequence <GPUTextureFormat >viewFormats = []; };
GPUTextureDescriptor
拥有如下成员:
size, 类型为 GPUExtent3D-
纹理的宽度、高度和深度或层数。
mipLevelCount,类型为 GPUIntegerCoordinate,默认值为1-
该纹理包含的 mip 级别数量。
sampleCount,类型为 GPUSize32,默认值为1-
纹理的采样数。当
sampleCount>1时,表示为多重采样纹理。 dimension,类型为 GPUTextureDimension,默认值为"2d"-
指定纹理是一维、二维数组层,还是三维纹理。
format,类型为 GPUTextureFormat-
纹理的格式。
usage,类型为 GPUTextureUsageFlags-
纹理允许的用途。
viewFormats,类型为 sequence<GPUTextureFormat>,默认值为[]-
指定在该纹理上调用
createView()时,允许作为format的格式(除了纹理实际的format外)。注意:添加格式到该列表可能会带来显著的性能影响,因此应避免不必要地添加格式。实际的性能影响高度依赖于目标系统;开发者需在多种系统上测试应用以了解具体影响。例如,在某些系统上,
format或viewFormats包含"rgba8unorm-srgb"时, 性能可能不如仅包含"rgba8unorm"的纹理。其他格式和格式组合在不同系统上也有类似注意事项。该列表的格式必须与纹理格式 兼容。
两个GPUTextureFormat, format 和 viewFormat,当且仅当满足以下条件时,纹理视图格式兼容(texture view format compatible):-
format 等于 viewFormat,或
-
format 与 viewFormat 唯一区别在于是否为
srgb格式(后缀为-srgb)。
-
enum {GPUTextureDimension "1d" ,"2d" ,"3d" , };
"1d"-
指定仅有宽度的一维纹理。
"1d"纹理不能有 mipmap,不能多重采样,不能使用压缩或深度/模板格式,也不能作为渲染目标。 "2d"-
指定具有宽度和高度,并可具有层的二维纹理。
"3d"-
指定具有宽度、高度和深度的三维纹理。
"3d"纹理不能多重采样,且其格式必须支持 3d 纹理(所有纯色格式和部分打包/压缩格式)。
typedef [EnforceRange ]unsigned long ; [GPUTextureUsageFlags Exposed =(Window ,Worker ),SecureContext ]namespace {GPUTextureUsage const GPUFlagsConstant COPY_SRC = 0x01;const GPUFlagsConstant COPY_DST = 0x02;const GPUFlagsConstant TEXTURE_BINDING = 0x04;const GPUFlagsConstant STORAGE_BINDING = 0x08;const GPUFlagsConstant RENDER_ATTACHMENT = 0x10; };
GPUTextureUsage
标志决定 GPUTexture
创建后可如何使用:
COPY_SRC-
该纹理可作为拷贝操作的源。(例如:作为
source参数传递给copyTextureToTexture()或copyTextureToBuffer()调用。) COPY_DST-
该纹理可作为拷贝或写入操作的目标。(例如:作为
destination参数传递给copyTextureToTexture()或copyBufferToTexture()调用,或作为writeTexture()的目标。) TEXTURE_BINDING-
该纹理可在着色器中作为采样纹理绑定(例如:作为
GPUTextureBindingLayout的绑定组项)。 STORAGE_BINDING-
该纹理可在着色器中作为存储纹理绑定(例如:作为
GPUStorageTextureBindingLayout的绑定组项)。 RENDER_ATTACHMENT-
该纹理可作为渲染通道中的颜色或深度/模板附件使用。(例如:作为
GPURenderPassColorAttachment.view或GPURenderPassDepthStencilAttachment.view。)
参数:
-
GPUTextureDimensiondimension -
GPUTextureDimensionsize
6.1.3. 纹理创建
createTexture(descriptor)-
创建一个
GPUTexture。调用对象:GPUDevicethis.参数:
参数列表: GPUDevice.createTexture(descriptor) 参数 类型 可为 null 可选 描述 descriptorGPUTextureDescriptor✘ ✘ 描述要创建的 GPUTexture。返回值:
GPUTexture内容时间线步骤:
-
? 校验 GPUExtent3D 形状(descriptor.
size)。 -
? 校验纹理格式所需特性 (descriptor.
format, this.[[device]])。 -
? 校验纹理格式所需特性 (对 descriptor.
viewFormats的每一项, this.[[device]])。 -
令 t = ! 创建新的 WebGPU 对象(this,
GPUTexture, descriptor)。 -
设 t.
depthOrArrayLayers= descriptor.size.depthOrArrayLayers。 -
设 t.
mipLevelCount= descriptor.mipLevelCount。 -
设 t.
sampleCount= descriptor.sampleCount。 -
在 this 的 设备时间线上执行初始化步骤。
-
返回 t。
设备时间线 初始化步骤:-
-
validating GPUTextureDescriptor(this, descriptor) 返回
true。
-
-
设 t.
[[viewFormats]]= descriptor.viewFormats。 -
为 t 创建设备分配,使每个块具有与全零比特表示的块等价的 texel 表示。
-
参数:
-
GPUDevicethis -
GPUTextureDescriptordescriptor
设备时间线步骤:
-
令 limits = this.
[[limits]]。 -
仅当所有如下要求都满足时返回
true,否则返回false:-
this 不得为 lost。
-
descriptor.
usage不得为 0。 -
descriptor.
size.width、 descriptor.size.height 和 descriptor.size.depthOrArrayLayers 必须均大于零。 -
descriptor.
mipLevelCount必须大于零。 -
descriptor.
sampleCount必须为 1 或 4。 -
如果 descriptor.
dimension为:"1d"-
-
descriptor.
size.width ≤ limits.maxTextureDimension1D。 -
descriptor.
size.depthOrArrayLayers 必须为 1。 -
descriptor.
sampleCount必须为 1。
-
"2d"-
-
descriptor.
size.width ≤ limits.maxTextureDimension2D。 -
descriptor.
size.height ≤ limits.maxTextureDimension2D。 -
descriptor.
size.depthOrArrayLayers ≤ limits.maxTextureArrayLayers。
-
"3d"-
-
descriptor.
size.width ≤ limits.maxTextureDimension3D。 -
descriptor.
size.height ≤ limits.maxTextureDimension3D。 -
descriptor.
size.depthOrArrayLayers ≤ limits.maxTextureDimension3D。 -
descriptor.
sampleCount必须为 1。 -
descriptor.
format必须支持"3d"纹理(参见 § 26.1 纹理格式能力)。
-
-
descriptor.
size.width 必须是 texel block width 的倍数。 -
descriptor.
size.height 必须是 texel block height 的倍数。 -
如果 descriptor.
sampleCount> 1:-
descriptor.
mipLevelCount必须为 1。 -
descriptor.
size.depthOrArrayLayers 必须为 1。 -
descriptor.
usage不得包含STORAGE_BINDING位。 -
descriptor.
usage必须包含RENDER_ATTACHMENT位。 -
descriptor.
format必须支持多重采样(参见 § 26.1 纹理格式能力)。
-
-
descriptor.
mipLevelCount必须小于等于 最大 mipLevel 数量(descriptor.dimension, descriptor.size)。 -
如果 descriptor.
usage包含RENDER_ATTACHMENT位: -
如果 descriptor.
usage包含STORAGE_BINDING位:-
descriptor.
format必须在 § 26.1.1 纯色格式表格中有STORAGE_BINDING支持(至少一种访问模式)。
-
-
对于 descriptor.
viewFormats中的每个 viewFormat,descriptor.format与 viewFormat 必须纹理视图格式兼容。注意:如果 viewFormat 与任何usage位均不兼容, 实现可考虑发出开发者可见的警告,因为该 viewFormat 将不可用。
-
const texture= gpuDevice. createTexture({ size: { width: 16 , height: 16 }, format: 'rgba8unorm' , usage: GPUTextureUsage. TEXTURE_BINDING, });
6.1.4. 纹理销毁
当应用不再需要某个 GPUTexture
时,可以通过调用 destroy()
主动失去对此对象的访问权,而无需等待垃圾回收。
注意: 这样允许用户代理在该 GPUTexture
相关的所有已提交操作完成后,回收其占用的 GPU 内存。
GPUTexture
拥有如下方法:
destroy()-
销毁该
GPUTexture。设备时间线步骤:-
将 this.
[[destroyed]]设为 true。
-
6.2. GPUTextureView
GPUTextureView
是对某个 纹理子资源
子集的视图,由特定 GPUTexture
定义。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUTextureView { };GPUTextureView includes GPUObjectBase ;
GPUTextureView
拥有如下不可变属性:
[[texture]],只读-
该视图所属的
GPUTexture。 [[descriptor]],只读-
描述此纹理视图的
GPUTextureViewDescriptor。所有
GPUTextureViewDescriptor的可选字段都有定义。 [[renderExtent]],只读-
对于可渲染视图,这是渲染时实际使用的
GPUExtent3DDict。注意:该范围取决于
baseMipLevel。
[[descriptor]]
为 desc,
是 view.[[texture]]
的子资源子集,其中每个子资源 s 满足:
-
s 的mipmap 级别满足:
≥ desc.
baseMipLevel且 < desc.baseMipLevel+ desc.mipLevelCount。 -
s 的数组层满足:
≥ desc.
baseArrayLayer且 < desc.baseArrayLayer+ desc.arrayLayerCount。
只有当两个 GPUTextureView
的子资源集合有交集时,它们才 视图别名(texture-view-aliasing)。
6.2.1. 纹理视图创建
dictionary :GPUTextureViewDescriptor GPUObjectDescriptorBase {GPUTextureFormat format ;GPUTextureViewDimension dimension ;GPUTextureUsageFlags usage = 0;GPUTextureAspect aspect = "all";GPUIntegerCoordinate baseMipLevel = 0;GPUIntegerCoordinate mipLevelCount ;GPUIntegerCoordinate baseArrayLayer = 0;GPUIntegerCoordinate arrayLayerCount ; };
GPUTextureViewDescriptor
拥有如下成员:
format,类型为 GPUTextureFormat-
纹理视图的格式。必须是纹理的
format或创建时指定的viewFormats之一。 dimension,类型为 GPUTextureViewDimension-
视图的维度。
usage,类型为 GPUTextureUsageFlags,默认值为0aspect,类型为 GPUTextureAspect,默认值为"all"-
视图可访问的纹理
aspect。 baseMipLevel,类型为 GPUIntegerCoordinate,默认值为0-
该视图可访问的第一个(最详细) mipmap 级别。
mipLevelCount,类型为 GPUIntegerCoordinate-
从
baseMipLevel开始,该视图可访问的 mipmap 级别数量。 baseArrayLayer,类型为 GPUIntegerCoordinate,默认值为0-
该视图可访问的第一个数组层索引。
arrayLayerCount,类型为 GPUIntegerCoordinate-
从
baseArrayLayer开始,该视图可访问的数组层数量。
enum {GPUTextureViewDimension "1d" ,"2d" ,"2d-array" ,"cube" ,"cube-array" ,"3d" , };
"1d"-
该视图作为一维图像。
对应的 WGSL 类型:
-
texture_1d -
texture_storage_1d
-
"2d"-
该视图作为单一的二维图像。
对应的 WGSL 类型:
-
texture_2d -
texture_storage_2d -
texture_multisampled_2d -
texture_depth_2d -
texture_depth_multisampled_2d
-
"2d-array"-
该视图作为二维图像数组。
对应的 WGSL 类型:
-
texture_2d_array -
texture_storage_2d_array -
texture_depth_2d_array
-
"cube"-
该视图作为立方体贴图。
该视图有 6 个数组层,分别对应立方体的 6 个面,顺序为
[+X, -X, +Y, -Y, +Z, -Z],朝向如下:立方体贴图各面。+U/+V 轴表示各面纹理坐标,也即每个面的 texel copy 内存布局。 注意:从内部看,这是一个左手坐标系,+X 右,+Y 上,+Z 前。
采样时会在立方体各面间无缝进行。
对应的 WGSL 类型:
-
texture_cube -
texture_depth_cube
-
"cube-array"-
该视图作为 n 个立方体贴图的打包数组,每个立方体 6 个数组层,总共 6n 层。
对应的 WGSL 类型:
-
texture_cube_array -
texture_depth_cube_array
-
"3d"-
该视图作为三维图像。
对应的 WGSL 类型:
-
texture_3d -
texture_storage_3d
-
每个 GPUTextureAspect 值对应一组 aspect。aspect 集合如下定义:
enum GPUTextureAspect {"all" ,"stencil-only" ,"depth-only" , };
"all"-
纹理格式的所有可用 aspect 都可被该视图访问。对于颜色格式,可访问 color aspect。对于深度-模板复合格式,可访问 depth 和 stencil 两个 aspect。只有单一 aspect 的深度或模板格式,仅可访问该 aspect。
"stencil-only"-
仅可访问深度或模板格式的 stencil aspect。
"depth-only"-
仅可访问深度或模板格式的 depth aspect。
createView(descriptor)-
创建一个
GPUTextureView。注意:默认情况下,createView()会创建一个可以表示整个纹理的视图。例如,在一个具有多个图层的"2d"纹理上调用createView()且未指定dimension时,将会创建一个"2d-array"GPUTextureView, 即使指定了arrayLayerCount为 1。对于那些在开发时无法确定图层数的来源创建的纹理,建议在调用
createView()时显式提供dimension,以确保着色器兼容性。调用对象:GPUTexturethis。参数:
参数列表: GPUTexture.createView(descriptor) 参数 类型 可为 null 可选 描述 descriptorGPUTextureViewDescriptor✘ ✔ 要创建的 GPUTextureView的描述。返回值: view,类型为
GPUTextureView。内容时间线步骤:
-
? 校验纹理格式所需特性(descriptor.
format, this.[[device]])。 -
令 view = ! 创建新的 WebGPU 对象(this,
GPUTextureView, descriptor)。 -
在 this 的 设备时间线上执行初始化步骤。
-
返回 view。
设备时间线 初始化步骤:-
将 descriptor 设为对 this 和 descriptor 执行 解析 GPUTextureViewDescriptor 默认值 的结果。
-
如下条件有任一不满足,产生校验错误,置为无效并返回 view。
-
this 可用于 valid to use with this.
[[device]]。 -
-
descriptor.
format必须等于 this.format或 this.[[viewFormats]]中的某一项。
否则:
-
descriptor.
format必须等于 解析 GPUTextureAspect(this.format, descriptor.aspect) 的结果。
-
-
如果 descriptor.
usage包含RENDER_ATTACHMENT位: -
如果 descriptor.
usage包含STORAGE_BINDING位:-
descriptor.
format必须在 § 26.1.1 纯色格式表格中有STORAGE_BINDING支持(至少一种访问模式)。
-
-
descriptor.
mipLevelCount必须大于 0。 -
descriptor.
baseMipLevel+ descriptor.mipLevelCount必须 ≤ this.mipLevelCount。 -
descriptor.
arrayLayerCount必须大于 0。 -
descriptor.
baseArrayLayer+ descriptor.arrayLayerCount必须 ≤ array layer count。 -
如 this.
sampleCount> 1,descriptor.dimension必须为"2d"。 -
如 descriptor.
dimension为:"1d"-
-
descriptor.
arrayLayerCount必须为1。
"2d"-
-
descriptor.
arrayLayerCount必须为1。
"2d-array""cube"-
-
descriptor.
arrayLayerCount必须为6。
"cube-array"-
-
descriptor.
arrayLayerCount必须为6的倍数。
"3d"-
-
descriptor.
arrayLayerCount必须为1。
-
-
令 view 为新的
GPUTextureView对象。 -
设 view.
[[texture]]= this。 -
设 view.
[[descriptor]]= descriptor。 -
如 descriptor.
usage包含RENDER_ATTACHMENT:-
令 renderExtent = 计算渲染范围([this.
width, this.height, this.depthOrArrayLayers], descriptor.baseMipLevel)。 -
设 view.
[[renderExtent]]= renderExtent。
-
-
GPUTextureView
texture 和 GPUTextureViewDescriptor
descriptor 执行 解析 GPUTextureViewDescriptor 默认值
时,执行如下设备时间线步骤:
-
令 resolved 为 descriptor 的副本。
-
如果 resolved.
mipLevelCount未提供: 设置 resolved.mipLevelCount= texture.mipLevelCount− resolved.baseMipLevel。 -
如果 resolved.
arrayLayerCount未提供,且 resolved.dimension为:"1d"、"2d"、或"3d"-
设置 resolved.
arrayLayerCount=1。 "cube"-
设置 resolved.
arrayLayerCount=6。 "2d-array"或"cube-array"-
设置 resolved.
arrayLayerCount= array layer count(texture) − resolved.baseArrayLayer。
-
返回 resolved。
GPUTexture
texture 的 array layer count,执行如下步骤:
-
若 texture.
dimension为:"1d"或"3d"-
返回
1。 "2d"-
返回 texture.
depthOrArrayLayers。
6.3. 纹理格式
格式名称指定了分量顺序、每个分量的位数和分量的数据类型。
-
r、g、b、a= 红、绿、蓝、alpha -
unorm= 无符号归一化 -
snorm= 有符号归一化 -
uint= 无符号整型 -
sint= 有符号整型 -
float= 浮点型
如果格式带有 -srgb 后缀,则在着色器中读取和写入颜色值时会应用 sRGB 的 gamma 到线性及其反向转换。压缩纹理格式由 特性 提供。命名应遵循此约定,以纹理名称为前缀,例如 etc2-rgba8unorm。
texel block(像素块)是像素格式
GPUTextureFormat
下纹理的单一可寻址元素,在区块压缩格式下为单一压缩块。
texel block width(像素块宽度)和 texel block height(像素块高度)指定了一个像素块的尺寸。
-
对于像素格式的
GPUTextureFormat,像素块宽度和像素块高度总是 1。 -
对于区块压缩的
GPUTextureFormat,像素块宽度为每个像素块一行的像素数,像素块高度为一块中的行数。详见§ 26.1 纹理格式能力,包含所有格式的取值列表。
texel block copy
footprint指的是某个GPUTextureFormat中某aspect在像素块拷贝时所占字节数(如适用)。
注意:
像素块内存开销是指存储一个GPUTextureFormat
像素块所需的字节数。不是所有格式都完全定义此值。该值仅作参考,非规范性内容。
enum { // 8-bit formatsGPUTextureFormat ,"r8unorm" ,"r8snorm" ,"r8uint" , // 16-bit formats"r8sint" ,"r16unorm" ,"r16snorm" ,"r16uint" ,"r16sint" ,"r16float" ,"rg8unorm" ,"rg8snorm" ,"rg8uint" , // 32-bit formats"rg8sint" ,"r32uint" ,"r32sint" ,"r32float" ,"rg16unorm" ,"rg16snorm" ,"rg16uint" ,"rg16sint" ,"rg16float" ,"rgba8unorm" ,"rgba8unorm-srgb" ,"rgba8snorm" ,"rgba8uint" ,"rgba8sint" ,"bgra8unorm" , // Packed 32-bit formats"bgra8unorm-srgb" ,"rgb9e5ufloat" ,"rgb10a2uint" ,"rgb10a2unorm" , // 64-bit formats"rg11b10ufloat" ,"rg32uint" ,"rg32sint" ,"rg32float" ,"rgba16unorm" ,"rgba16snorm" ,"rgba16uint" ,"rgba16sint" , // 128-bit formats"rgba16float" ,"rgba32uint" ,"rgba32sint" , // Depth/stencil formats"rgba32float" ,"stencil8" ,"depth16unorm" ,"depth24plus" ,"depth24plus-stencil8" , // "depth32float-stencil8" feature"depth32float" , // BC compressed formats usable if "texture-compression-bc" is both // supported by the device/user agent and enabled in requestDevice."depth32float-stencil8" ,"bc1-rgba-unorm" ,"bc1-rgba-unorm-srgb" ,"bc2-rgba-unorm" ,"bc2-rgba-unorm-srgb" ,"bc3-rgba-unorm" ,"bc3-rgba-unorm-srgb" ,"bc4-r-unorm" ,"bc4-r-snorm" ,"bc5-rg-unorm" ,"bc5-rg-snorm" ,"bc6h-rgb-ufloat" ,"bc6h-rgb-float" ,"bc7-rgba-unorm" , // ETC2 compressed formats usable if "texture-compression-etc2" is both // supported by the device/user agent and enabled in requestDevice."bc7-rgba-unorm-srgb" ,"etc2-rgb8unorm" ,"etc2-rgb8unorm-srgb" ,"etc2-rgb8a1unorm" ,"etc2-rgb8a1unorm-srgb" ,"etc2-rgba8unorm" ,"etc2-rgba8unorm-srgb" ,"eac-r11unorm" ,"eac-r11snorm" ,"eac-rg11unorm" , // ASTC compressed formats usable if "texture-compression-astc" is both // supported by the device/user agent and enabled in requestDevice."eac-rg11snorm" ,"astc-4x4-unorm" ,"astc-4x4-unorm-srgb" ,"astc-5x4-unorm" ,"astc-5x4-unorm-srgb" ,"astc-5x5-unorm" ,"astc-5x5-unorm-srgb" ,"astc-6x5-unorm" ,"astc-6x5-unorm-srgb" ,"astc-6x6-unorm" ,"astc-6x6-unorm-srgb" ,"astc-8x5-unorm" ,"astc-8x5-unorm-srgb" ,"astc-8x6-unorm" ,"astc-8x6-unorm-srgb" ,"astc-8x8-unorm" ,"astc-8x8-unorm-srgb" ,"astc-10x5-unorm" ,"astc-10x5-unorm-srgb" ,"astc-10x6-unorm" ,"astc-10x6-unorm-srgb" ,"astc-10x8-unorm" ,"astc-10x8-unorm-srgb" ,"astc-10x10-unorm" ,"astc-10x10-unorm-srgb" ,"astc-12x10-unorm" ,"astc-12x10-unorm-srgb" ,"astc-12x12-unorm" , };"astc-12x12-unorm-srgb"
"depth24plus"
和 "depth24plus-stencil8"
格式的 depth 分量可以实现为 24 位深度值,也可以实现为
"depth32float"
值。
stencil8
格式可以实现为真正的 "stencil8",也可以实现为 "depth24stencil8"(此时 depth aspect 是隐藏且不可访问的)。
-
对于 24 位深度,1 ULP 恒等于 1 / (224 − 1)。
-
对于 depth32float,1 ULP 可变,但不会大于 1 / (224)。
格式为 可渲染(renderable)格式,若其为 颜色可渲染格式,或为深度或模板格式。
若某格式在 § 26.1.1 普通颜色格式 中标记了 RENDER_ATTACHMENT
能力,则为颜色可渲染格式。其它格式不是颜色可渲染格式。所有深度或模板格式均为可渲染格式。
可渲染格式若可与渲染管线混合(blending)使用,则也是 可混合(blendable)格式。详见 § 26.1 纹理格式能力。
格式为 可过滤(filterable)格式,若其支持 GPUTextureSampleType
的 "float"(不只是
"unfilterable-float"),即该格式可用于
"filtering"
类型 GPUSampler。详见
§ 26.1 纹理格式能力。
参数:
-
GPUTextureFormatformat -
GPUTextureAspectaspect
返回值: GPUTextureFormat
或 null
-
若 aspect 为:
"all"-
返回 format。
"depth-only""stencil-only"-
若 format 为 depth-stencil-format: 返回 aspect-specific format,详见 § 26.1.2 深度/模板格式;如该 format 不含此 aspect,返回
null。
-
返回
null。
某些纹理格式的使用需要在 GPUDevice
上启用特性。由于新格式可添加到规范中,所以实现可能不知道这些枚举值。为保证一致性,尝试在未启用相关特性的设备上使用需要特性的格式时会抛出异常,这与实现本身不识别该格式时行为一致。
见 § 26.1 纹理格式能力,了解哪些 GPUTextureFormat
需要特性支持。
GPUTextureFormat
format, 逻辑device
device)
-
若 format 需要特性且 device.
[[features]]不包含该特性:-
抛出
TypeError。
-
6.4. GPUExternalTexture
GPUExternalTexture
是一个可采样的二维纹理,用于封装外部视频帧。它是不可变快照;其内容不会随时间变化,无论是来自 WebGPU 内部(仅可采样)还是外部(如视频帧推进)。
GPUExternalTexture
可通过
externalTexture
绑定组布局成员作为绑定项绑定到绑定组。注意该成员会占用多个绑定槽,详见相关定义。
GPUExternalTexture
可以在不复制导入源的情况下实现,但这取决于实现定义因素。底层表示的所有权可能是独占的,也可能与其他所有者(如视频解码器)共享,但这对应用透明。
外部纹理的底层表示不可见(除精确采样行为外),但通常包括:
-
最多三组二维平面(如 RGBA、Y+UV、Y+U+V)。
-
用于采样前坐标转换的元数据(裁剪和旋转)。
-
用于输出到目标色彩空间的元数据(矩阵、gamma、3D LUT)。
实现内部使用的配置在不同时间、系统、用户代理、媒体源、甚至同一个视频资源的帧之间可能不一致。为适应各种可能的实现,每个外部纹理绑定都会保守地占用:
-
三个采样纹理绑定(最多 3 个平面);
-
一个 3D LUT 的采样纹理绑定;
-
一个采样器绑定(用于采样 3D LUT);
-
一个元数据的 uniform buffer 绑定。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUExternalTexture { };GPUExternalTexture includes GPUObjectBase ;
GPUExternalTexture
拥有如下不可变属性:
[[descriptor]],类型为GPUExternalTextureDescriptor,只读-
创建该纹理时所用的 descriptor。
GPUExternalTexture
拥有如下不可变属性:
[[expired]],类型为boolean,初始值为false-
表示对象是否已过期(不可再用)。
注意: 与
[[destroyed]]不同,该值可从true恢复到false。
6.4.1. 导入外部纹理
外部纹理可通过 importExternalTexture()
从外部视频对象创建。
由 HTMLVideoElement
创建的外部纹理会在导入后自动过期(即销毁),而不像其他资源那样需要手动或等垃圾回收。外部纹理过期时,其 [[expired]]
属性变为 true。
由 VideoFrame
创建的外部纹理会在且仅在源 VideoFrame
被关闭时过期(无论是通过 close()
还是其他方式)。
注意:如 decode()
所述,开发者应在输出 VideoFrame
上调用 close(),以避免解码器阻塞。如果导入的
VideoFrame
被丢弃但未关闭,导入的 GPUExternalTexture
会保持其存活,直到它也被丢弃。只有两者都被丢弃后,VideoFrame
才能被垃圾回收。由于垃圾回收不可预测,这仍然可能导致解码器阻塞。
一旦 GPUExternalTexture
过期,必须再次调用 importExternalTexture()。不过,用户代理可能会取消过期,返回同一个
GPUExternalTexture,而不是新建一个。通常只有应用调度与视频帧率同步(如用
requestVideoFrameCallback())时才会避免返回同一个对象。如返回同一对象,二者相等,且引用旧对象的 GPUBindGroup、GPURenderBundle
等依然有效。
dictionary :GPUExternalTextureDescriptor GPUObjectDescriptorBase {required (HTMLVideoElement or VideoFrame )source ;PredefinedColorSpace colorSpace = "srgb"; };
GPUExternalTextureDescriptor
字典含如下成员:
source,类型为(HTMLVideoElement or VideoFrame)-
要导入为外部纹理的视频源。源尺寸详见外部源尺寸表。
colorSpace,类型为 PredefinedColorSpace,默认"srgb"-
读取时,
source的图像内容会被转换到的色彩空间。
importExternalTexture(descriptor)-
创建一个包裹所提供图像源的
GPUExternalTexture。调用对象:GPUDevicethis。参数:
GPUDevice.importExternalTexture(descriptor) 方法参数 参数 类型 可为 null 可选 描述 descriptorGPUExternalTextureDescriptor✘ ✘ 提供外部图像源对象及相关创建选项。 内容时间线步骤:
-
令 source = descriptor.
source。 -
若 source 的当前图像内容与最近一次用同一 descriptor(忽略
label)调用importExternalTexture()相同,且用户代理选择复用:-
令 previousResult 为上次返回的
GPUExternalTexture。 -
置 previousResult.
[[expired]]为false,恢复底层资源所有权。 -
令 result = previousResult。
注意:这样应用可检测重复导入,避免重新创建依赖对象(如
GPUBindGroup)。实现仍需支持同一帧被多个GPUExternalTexture包裹,因为导入元数据如colorSpace可变。否则:
-
如 source 不为 origin-clean,抛出
SecurityError并返回。 -
如 usability 非
good: -
令 data 为将 source 当前图像内容转换为 descriptor.
colorSpace(未预乘 alpha)所得。此 可能导致超出 [0,1] 范围的值。如需裁剪,可采样后再做。
注意:虽描述为拷贝,也可实现为指向只读底层数据和元数据的引用。
-
令 result 为包裹 data 的新
GPUExternalTexture。
-
-
如 source 是
HTMLVideoElement,排队自动过期任务,内容包括:-
置 result.
[[expired]]为true,释放底层资源所有权。
注意:应在采样同一任务中导入
HTMLVideoElement,建议结合requestVideoFrameCallback或requestAnimationFrame()使用,否则纹理可能在应用用完前被销毁。 -
-
如 source 是
VideoFrame,则当 source 关闭时,执行:-
置 result.
[[expired]]为true。
-
-
返回 result。
-
const videoElement= document. createElement( 'video' ); // ... 设置 videoElement,等待其就绪 ... function frame() { requestAnimationFrame( frame); // 每帧都重新导入视频,因为导入的纹理很可能已过期。 // 浏览器可能缓存并复用旧帧,如复用会返回同一个 GPUExternalTexture。 // 这时旧的 bind group 依然有效。 const externalTexture= gpuDevice. importExternalTexture({ source: videoElement}); // ... 使用 externalTexture 渲染 ... } requestAnimationFrame( frame);
requestVideoFrameCallback,则以视频帧率渲染 video 元素外部纹理:
const videoElement= document. createElement( 'video' ); // ... 设置 videoElement ... function frame() { videoElement. requestVideoFrameCallback( frame); // 帧已推进,需重新导入 const externalTexture= gpuDevice. importExternalTexture({ source: videoElement}); // ... 使用 externalTexture 渲染 ... } videoElement. requestVideoFrameCallback( frame);
6.5. 采样外部纹理绑定
externalTexture
绑定点允许绑定 GPUExternalTexture
对象(来自视频等动态图像源)。同时也支持 GPUTexture 和
GPUTextureView。
注意:
当 GPUTexture 或
GPUTextureView
被绑定到 externalTexture
绑定点时,其行为等同于单一 RGBA plane、无裁剪、无旋转、无颜色转换的 GPUExternalTexture。
外部纹理在 WGSL 中以 texture_external 表示,可通过 textureLoad 和
textureSampleBaseClampToEdge 读取。
textureSampleBaseClampToEdge 所用的 sampler 用于采样底层纹理。
当绑定资源类型为 GPUExternalTexture
时,采样结果在 colorSpace
设置的色彩空间中。对于具体外部纹理,采样器(及其过滤操作)是在底层值转换到指定色彩空间之前还是之后应用,是实现相关的。
注意: 若内部表示为 RGBA 平面,则采样行为与常规 2D 纹理一致。若有多个底层平面(如 Y+UV),采样器会分别对每个底层纹理采样,再进行 YUV 到指定色彩空间的转换。
7. 采样器(Samplers)
7.1. GPUSampler
GPUSampler
用于编码可在着色器中解释纹理资源数据的变换与过滤信息。
GPUSampler
通过 createSampler()
创建。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUSampler { };GPUSampler includes GPUObjectBase ;
GPUSampler
拥有如下不可变属性:
[[descriptor]],类型为GPUSamplerDescriptor,只读-
创建
GPUSampler时使用的GPUSamplerDescriptor。 [[isComparison]],类型为boolean,只读-
该
GPUSampler是否为比较采样器。 [[isFiltering]],类型为boolean,只读-
该
GPUSampler是否对纹理的多个采样结果加权(即支持过滤)。
7.1.1. GPUSamplerDescriptor
GPUSamplerDescriptor
指定了创建 GPUSampler
时应使用的选项。
dictionary :GPUSamplerDescriptor GPUObjectDescriptorBase {GPUAddressMode addressModeU = "clamp-to-edge";GPUAddressMode addressModeV = "clamp-to-edge";GPUAddressMode addressModeW = "clamp-to-edge";GPUFilterMode magFilter = "nearest";GPUFilterMode minFilter = "nearest";GPUMipmapFilterMode mipmapFilter = "nearest";float lodMinClamp = 0;float lodMaxClamp = 32;GPUCompareFunction compare ; [Clamp ]unsigned short maxAnisotropy = 1; };
addressModeU, 类型为 GPUAddressMode,默认值为"clamp-to-edge"addressModeV, 类型为 GPUAddressMode,默认值为"clamp-to-edge"addressModeW, 类型为 GPUAddressMode,默认值为"clamp-to-edge"-
分别指定纹理在宽度、高度和深度坐标方向上的
地址模式。 magFilter, 类型为 GPUFilterMode,默认值为"nearest"-
指定当采样区域小于等于一个纹素时的采样行为。
minFilter, 类型为 GPUFilterMode,默认值为"nearest"-
指定当采样区域大于一个纹素时的采样行为。
mipmapFilter, 类型为 GPUMipmapFilterMode,默认值为"nearest"-
指定在 mipmap 层级之间采样时的行为。
lodMinClamp, 类型为 float,默认值为0lodMaxClamp, 类型为 float,默认值为32-
分别指定采样纹理时内部使用的最小和最大细节层级(LOD)。
compare, 类型为 GPUCompareFunction-
如果提供,则采样器将被设为比较采样器,并使用指定的
GPUCompareFunction。注意:比较采样器可以使用过滤,但采样结果依赖于实现,可能不同于普通过滤规则。
maxAnisotropy, 类型为 unsigned short,默认值为1-
指定采样器使用的最大各向异性值上限。当
maxAnisotropy大于 1 且实现支持时,启用各向异性过滤。各向异性过滤可提高在斜视角下采样纹理的图像质量。更高的
maxAnisotropy值表示过滤时支持的最大各向异性比。
细节层级(Level of detail,LOD) 描述了采样纹理时选用的 mip 层级。可以通过 textureSampleLevel 等着色器方法显式指定,也可以由纹理坐标导数隐式决定。
注意:参考 Scale Factor Operation, LOD Operation and Image Level Selection(Vulkan 1.3 规范)了解隐式 LOD 如何计算。
GPUAddressMode
描述了采样纹理时纹素超出纹理边界时的采样器行为。
enum {GPUAddressMode "clamp-to-edge" ,"repeat" ,"mirror-repeat" , };
"clamp-to-edge"-
纹理坐标被限制在 0.0 到 1.0 区间内。
"repeat"-
纹理坐标将在纹理另一侧环绕。
"mirror-repeat"-
纹理坐标将在纹理另一侧环绕,但当坐标的整数部分为奇数时,纹理会翻转。
GPUFilterMode
和 GPUMipmapFilterMode
描述采样区域不正好覆盖一个纹素时采样器的行为。
注意:参考 Texel Filtering(Vulkan 1.3 规范)了解采样器如何确定在不同过滤模式下采样哪些纹素。
enum {GPUFilterMode "nearest" ,"linear" , };enum {GPUMipmapFilterMode ,"nearest" , };"linear"
"nearest"-
返回最接近纹理坐标的纹素值。
"linear"-
每个维度选择两个纹素,并返回它们值的线性插值。
GPUCompareFunction
指定比较采样器的行为。如果在着色器中使用比较采样器,depth_ref 会与获取到的纹素值进行比较,生成比较结果(通过则为 1.0f,未通过为
0.0f)。
比较后,如果启用纹理过滤,则会进行过滤步骤,使多个比较结果混合,结果在 [0, 1]
区间。过滤应当如常处理,但可能以更低精度或不做混合。
enum {GPUCompareFunction "never" ,"less" ,"equal" ,"less-equal" ,"greater" ,"not-equal" ,"greater-equal" ,"always" , };
"never"-
比较测试始终不通过。
"less"-
当提供值小于采样值时,比较测试通过。
"equal"-
当提供值等于采样值时,比较测试通过。
"less-equal"-
当提供值小于等于采样值时,比较测试通过。
"greater"-
当提供值大于采样值时,比较测试通过。
"not-equal"-
当提供值不等于采样值时,比较测试通过。
"greater-equal"-
当提供值大于等于采样值时,比较测试通过。
"always"-
比较测试始终通过。
7.1.2. 采样器创建
createSampler(descriptor)-
创建一个
GPUSampler。调用对象:GPUDevicethis。参数:
参数表:GPUDevice.createSampler(descriptor) 方法。 参数名 类型 可为 null 可选 描述 descriptorGPUSamplerDescriptor✘ ✔ 要创建的 GPUSampler的描述信息。返回:
GPUSampler内容时间线步骤:
-
令 s = ! 创建新的 WebGPU 对象(this,
GPUSampler, descriptor)。 -
在 设备时间线 上对 this 执行 初始化步骤。
-
返回 s。
设备时间线 初始化步骤:-
如果下列任一条件不满足,则生成校验错误,使 s 无效 并返回。
-
this 不能被丢失。
-
descriptor.
lodMinClamp≥ 0。 -
descriptor.
lodMaxClamp≥ descriptor.lodMinClamp。 -
descriptor.
maxAnisotropy≥ 1。注意:大多数实现支持
maxAnisotropy取值范围为 1 到 16(含)。所提供的maxAnisotropy值会被限制在平台支持的最大值。 -
如果 descriptor.
maxAnisotropy> 1:-
descriptor.
magFilter, descriptor.minFilter, 和 descriptor.mipmapFilter必须都为"linear"。
-
-
-
设置 s.
[[descriptor]]为 descriptor。 -
如果 s.
[[descriptor]]的compare属性为null或未定义,则设置 s.[[isComparison]]为false。否则设置为true。 -
如果
minFilter、magFilter、mipmapFilter都不是"linear",则设置 s.[[isFiltering]]为false;否则设为true。
-
GPUSampler:
const sampler= gpuDevice. createSampler({ addressModeU: 'repeat' , addressModeV: 'repeat' , magFilter: 'linear' , minFilter: 'linear' , mipmapFilter: 'linear' , });
8. 资源绑定
8.1. GPUBindGroupLayout
GPUBindGroupLayout
定义了绑定在 GPUBindGroup
中的一组资源与着色器阶段可访问性的接口关系。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUBindGroupLayout { };GPUBindGroupLayout includes GPUObjectBase ;
GPUBindGroupLayout
具有以下不可变属性:
[[descriptor]],类型为GPUBindGroupLayoutDescriptor, 只读
8.1.1. 绑定组布局创建
GPUBindGroupLayout
通过 GPUDevice.createBindGroupLayout()
创建。
dictionary :GPUBindGroupLayoutDescriptor GPUObjectDescriptorBase {required sequence <GPUBindGroupLayoutEntry >entries ; };
GPUBindGroupLayoutDescriptor
字典包含以下成员:
entries,类型为 sequence<GPUBindGroupLayoutEntry>-
用于描述绑定组内着色器资源绑定的条目列表。
GPUBindGroupLayoutEntry
描述了要包含在 GPUBindGroupLayout
中的单个着色器资源绑定。
dictionary {GPUBindGroupLayoutEntry required GPUIndex32 binding ;required GPUShaderStageFlags visibility ;GPUBufferBindingLayout buffer ;GPUSamplerBindingLayout sampler ;GPUTextureBindingLayout texture ;GPUStorageTextureBindingLayout storageTexture ;GPUExternalTextureBindingLayout externalTexture ; };
GPUBindGroupLayoutEntry
字典包含以下成员:
binding,类型为 GPUIndex32-
绑定组布局内资源绑定的唯一标识符,对应于
GPUBindGroupEntry.binding以及GPUShaderModule中的 @binding 属性。 visibility,类型为 GPUShaderStageFlags-
GPUShaderStage各成员组成的位集合。每个被设置的位表示该资源在对应着色器阶段可访问。 buffer,类型为 GPUBufferBindingLayoutsampler,类型为 GPUSamplerBindingLayouttexture,类型为 GPUTextureBindingLayoutstorageTexture,类型为 GPUStorageTextureBindingLayoutexternalTexture,类型为 GPUExternalTextureBindingLayout-
这些成员中必须且只能设置一个,表示绑定类型。该成员的内容指定该类型的选项。
createBindGroup()中对应的资源必须为该绑定类型的binding resource type。
typedef [EnforceRange ]unsigned long ; [GPUShaderStageFlags Exposed =(Window ,Worker ),SecureContext ]namespace {GPUShaderStage const GPUFlagsConstant VERTEX = 0x1;const GPUFlagsConstant FRAGMENT = 0x2;const GPUFlagsConstant COMPUTE = 0x4; };
GPUShaderStage
包含以下标志,指示此 GPUBindGroupEntry
对应的 GPUBindGroupLayoutEntry
可见于哪些着色器阶段:
VERTEX-
绑定组条目在顶点着色器中可访问。
FRAGMENT-
绑定组条目在片元着色器中可访问。
COMPUTE-
绑定组条目在计算着色器中可访问。
GPUBindGroupLayoutEntry
的 binding member 由其定义的成员决定:
buffer、
sampler、
texture、
storageTexture,
或 externalTexture。
每个 GPUBindGroupLayoutEntry
只能定义一个成员。
每个成员有其对应的 GPUBindingResource
类型,每个绑定类型对应一个内部用法,见下表:
GPUBindGroupLayoutEntry
值 entries
超出绑定槽限制(exceeds the binding slot limits),指当用于某一限制的槽数超过 支持的限制 limits
中的支持值时。
每个 entry 可能会占用多个限制的多个槽。
设备时间线步骤:
-
对于 entries 中的每个 entry,如果:
- entry.
buffer?.type为"uniform"且 entry.buffer?.hasDynamicOffset为true -
视为使用了 1 个
maxDynamicUniformBuffersPerPipelineLayout槽。 - entry.
buffer?.type为"storage"且 entry.buffer?.hasDynamicOffset为true -
视为使用了 1 个
maxDynamicStorageBuffersPerPipelineLayout槽。
- entry.
-
对于每个着色器阶段 stage,即 «
VERTEX,FRAGMENT,COMPUTE»:-
对于每个 entries 中 entry,若其 entry.
visibility包含 stage,则:- entry.
buffer?.type为"uniform" -
视为使用了 1 个
maxUniformBuffersPerShaderStage槽。 - entry.
buffer?.type为"storage"或"read-only-storage" -
视为使用了 1 个
maxStorageBuffersPerShaderStage槽。 - entry.
sampler存在(provided) -
视为使用了 1 个
maxSamplersPerShaderStage槽。 - entry.
texture存在 -
视为使用了 1 个
maxSampledTexturesPerShaderStage槽。 - entry.
storageTexture存在 -
视为使用了 1 个
maxStorageTexturesPerShaderStage槽。 - entry.
externalTexture存在 -
视为使用 4 个
maxSampledTexturesPerShaderStage槽, 1 个maxSamplersPerShaderStage槽,以及 1 个maxUniformBuffersPerShaderStage槽。注意:详见
GPUExternalTexture相关说明。
- entry.
-
enum {GPUBufferBindingType ,"uniform" ,"storage" , };"read-only-storage" dictionary {GPUBufferBindingLayout GPUBufferBindingType type = "uniform";boolean hasDynamicOffset =false ;GPUSize64 minBindingSize = 0; };
GPUBufferBindingLayout
字典包含以下成员:
type,类型为 GPUBufferBindingType,默认值为"uniform"-
指示绑定到该绑定点的缓冲区所需的类型。
hasDynamicOffset,类型为 boolean,默认值为false-
指示该绑定是否需要动态偏移。
minBindingSize,类型为 GPUSize64,默认值为0-
指示与该绑定点使用的缓冲区绑定的最小
size。在
createBindGroup()中总会对绑定进行该尺寸的校验。若该值不为
0,则管线创建时还会额外校验 该值是否大于等于变量的最小缓冲区绑定大小。若该值为
0,则管线创建时忽略,转而由 draw/dispatch 指令 校验绑定组中每个绑定是否满足变量的最小缓冲区绑定大小。注意: 理论上,类似的运行时校验也可用于其它用于早期校验的绑定相关字段,如
sampleType和format,但目前仅在管线创建时校验。 不过这样的运行时校验可能开销大或不必要复杂,因此仅对minBindingSize启用,因为它对易用性影响最大。
enum {GPUSamplerBindingType ,"filtering" ,"non-filtering" , };"comparison" dictionary {GPUSamplerBindingLayout GPUSamplerBindingType type = "filtering"; };
GPUSamplerBindingLayout
字典包含以下成员:
type,类型为 GPUSamplerBindingType,默认值为"filtering"-
指示绑定到此绑定点所需采样器的类型。
enum {GPUTextureSampleType ,"float" ,"unfilterable-float" ,"depth" ,"sint" , };"uint" dictionary {GPUTextureBindingLayout GPUTextureSampleType sampleType = "float";GPUTextureViewDimension viewDimension = "2d";boolean multisampled =false ; };
GPUTextureBindingLayout
字典包含以下成员:
sampleType,类型为 GPUTextureSampleType,默认值为"float"-
指示绑定到该绑定点的纹理视图所需的类型。
viewDimension,类型为 GPUTextureViewDimension,默认值为"2d"-
指示绑定到该绑定点的纹理视图所需的
dimension。 multisampled,类型为 boolean,默认值为false-
指示绑定到该绑定点的纹理视图是否必须为多重采样。
enum {GPUStorageTextureAccess ,"write-only" ,"read-only" , };"read-write" dictionary {GPUStorageTextureBindingLayout GPUStorageTextureAccess access = "write-only";required GPUTextureFormat format ;GPUTextureViewDimension viewDimension = "2d"; };
GPUStorageTextureBindingLayout
字典包含以下成员:
access,类型为 GPUStorageTextureAccess,默认值为"write-only"-
此绑定的访问模式,指示可读性和可写性。
format,类型为 GPUTextureFormat-
绑定到此绑定点的纹理视图所需的
format。 viewDimension,类型为 GPUTextureViewDimension,默认值为"2d"-
绑定到此绑定点的纹理视图所需的
dimension。
dictionary { };GPUExternalTextureBindingLayout
GPUBindGroupLayout
对象拥有以下设备时间线属性:
[[entryMap]],类型为 有序映射<GPUSize32,GPUBindGroupLayoutEntry>, 只读-
指向该
GPUBindGroupLayout所描述的GPUBindGroupLayoutEntry的绑定索引映射表。 [[dynamicOffsetCount]],类型为GPUSize32, 只读-
此
GPUBindGroupLayout中具有动态偏移的缓冲区绑定数量。 [[exclusivePipeline]],类型为GPUPipelineBase? ,只读-
如果是作为默认管线布局一部分创建的,则为创建该
GPUBindGroupLayout的管线。若不为null,则用该GPUBindGroupLayout创建的GPUBindGroup只能与指定的GPUPipelineBase一起使用。
createBindGroupLayout(descriptor)-
创建一个
GPUBindGroupLayout。调用对象:GPUDevicethis。参数:
GPUDevice.createBindGroupLayout(descriptor) 方法参数。 参数 类型 可为 null 可选 描述 descriptorGPUBindGroupLayoutDescriptor✘ ✘ 要创建的 GPUBindGroupLayout的描述信息。内容时间线步骤:
-
对于 descriptor.
entries中的每个GPUBindGroupLayoutEntryentry:-
如果 entry.
storageTexture已提供:-
? 校验纹理格式所需特性,针对 entry.
storageTexture.format及 this.[[device]]。
-
-
-
令 layout = ! 创建 WebGPU 对象(this,
GPUBindGroupLayout, descriptor)。 -
在 this 的 设备时间线上执行 初始化步骤。
-
返回 layout。
设备时间线 初始化步骤:-
若下列任意条件不满足,则生成校验错误,使 layout 无效并返回。
-
this 不能被丢失。
-
令 limits = this.
[[device]].[[limits]]。 -
descriptor 中每个 entry 的
binding必须唯一。 -
descriptor 中每个 entry 的
binding必须小于 limits.maxBindingsPerBindGroup。 -
descriptor.
entries必须不能超出绑定槽限制(exceed the binding slot limits) limits。 -
对于 descriptor.
entries中的每个GPUBindGroupLayoutEntryentry:-
entry.
buffer、 entry.sampler、 entry.texture、 entry.storageTexture、 entry.externalTexture必须且只能有一个 被提供。 -
entry.
visibility仅包含GPUShaderStage中定义的位。 -
若 entry.
visibility包含VERTEX:-
若 entry.
buffer被提供, entry.buffer.type必须为"uniform"或"read-only-storage"。 -
若 entry.
storageTexture被提供, entry.storageTexture.access必须为"read-only"。
-
-
若 entry.
texture?.multisampled为true:-
entry.
texture.viewDimension为"2d"。 -
entry.
texture.sampleType不能为"float"。
-
-
若 entry.
storageTexture被提供:-
entry.
storageTexture.viewDimension不能为"cube"或"cube-array"。 -
entry.
storageTexture.format必须为可支持所给 entry.storageTexture.access存储用法的格式,详见 § 26.1.1 普通颜色格式表格。
-
-
-
-
设置 layout.
[[descriptor]]为 descriptor。 -
设置 layout.
[[dynamicOffsetCount]]为 descriptor 中buffer被提供,且buffer.hasDynamicOffset为true的条目数量。 -
设置 layout.
[[exclusivePipeline]]为null。 -
对于 descriptor.
entries中的每个GPUBindGroupLayoutEntryentry:-
以 entry.
binding作为 key,将 entry 插入 layout.[[entryMap]]。
-
-
8.1.2. 兼容性
GPUBindGroupLayout
对象 a 和 b 被认为是 组等价(group-equivalent)
当且仅当满足以下所有条件:
-
对任意绑定号 binding,有如下条件之一满足:
-
在 a.
[[entryMap]]和 b.[[entryMap]]中都不存在; -
a.
[[entryMap]][binding] == b.[[entryMap]][binding]
-
如果绑定组布局是组等价的,则它们可以在所有场合下互换使用。
8.2. GPUBindGroup
GPUBindGroup
定义了一组要共同绑定的资源,以及这些资源在着色器阶段的使用方式。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUBindGroup { };GPUBindGroup includes GPUObjectBase ;
GPUBindGroup
具有以下设备时间线属性:
[[layout]],类型为GPUBindGroupLayout, 只读-
与该
GPUBindGroup相关联的GPUBindGroupLayout。 [[entries]],类型为 sequence<GPUBindGroupEntry>, 只读-
该
GPUBindGroup所描述的所有GPUBindGroupEntry集合。 [[usedResources]],类型为 usage scope,只读
GPUBindGroup
bindGroup 的 已绑定缓冲区范围(bound buffer ranges),
给定 list<GPUBufferDynamicOffset> dynamicOffsets,按如下方式计算:
-
令 result 为新的 集合<(
GPUBindGroupLayoutEntry,GPUBufferBinding)>。 -
令 dynamicOffsetIndex 为 0。
-
对 bindGroup.
[[entries]]中每个GPUBindGroupEntrybindGroupEntry(按 bindGroupEntry.binding排序):-
令 bindGroupLayoutEntry = bindGroup.
[[layout]].[[entryMap]][bindGroupEntry.binding]。 -
令 bound = get as buffer binding(bindGroupEntry.
resource)。 -
若 bindGroupLayoutEntry.
buffer.hasDynamicOffset:-
将 bound.
offset增加 dynamicOffsets[dynamicOffsetIndex]。 -
dynamicOffsetIndex 增加 1。
-
-
将 (bindGroupLayoutEntry, bound) 添加到 result。
-
-
返回 result。
8.2.1. 绑定组创建
GPUBindGroup
通过 GPUDevice.createBindGroup()
创建。
dictionary :GPUBindGroupDescriptor GPUObjectDescriptorBase {required GPUBindGroupLayout layout ;required sequence <GPUBindGroupEntry >entries ; };
GPUBindGroupDescriptor
字典包含以下成员:
layout,类型为 GPUBindGroupLayout-
该绑定组条目所遵循的
GPUBindGroupLayout。 entries,类型为 sequence<GPUBindGroupEntry>-
描述要为
layout所描述的每个绑定暴露给着色器的资源条目的列表。
typedef (GPUSampler or GPUTexture or GPUTextureView or GPUBuffer or GPUBufferBinding or GPUExternalTexture );GPUBindingResource dictionary {GPUBindGroupEntry required GPUIndex32 binding ;required GPUBindingResource resource ; };
GPUBindGroupEntry
描述要绑定到 GPUBindGroup
的单个资源,包含以下成员:
binding,类型为 GPUIndex32-
该绑定组内资源绑定的唯一标识符,对应于
GPUBindGroupLayoutEntry.binding以及GPUShaderModule中的 @binding 属性。 resource,类型为 GPUBindingResource-
要绑定的资源,可以是
GPUSampler、GPUTexture、GPUTextureView、GPUBuffer、GPUBufferBinding, 或GPUExternalTexture。
GPUBindGroupEntry
具有以下设备时间线属性:
[[prevalidatedSize]],类型为boolean-
该绑定条目在创建时其缓冲区大小是否已被校验。
dictionary {GPUBufferBinding required GPUBuffer buffer ;GPUSize64 offset = 0;GPUSize64 size ; };
GPUBufferBinding
描述了要作为资源绑定的缓冲区及可选范围,包含以下成员:
buffer,类型为 GPUBuffer-
要绑定的
GPUBuffer。 offset,类型为 GPUSize64,默认值为0-
以字节为单位,
buffer起始到绑定暴露给着色器的范围起点的偏移量。 size,类型为 GPUSize64
createBindGroup(descriptor)-
创建一个
GPUBindGroup。调用对象:GPUDevicethis。参数:
GPUDevice.createBindGroup(descriptor) 方法参数。 参数 类型 可为 null 可选 描述 descriptorGPUBindGroupDescriptor✘ ✘ 要创建的 GPUBindGroup的描述信息。返回:
GPUBindGroup内容时间线步骤:
-
令 bindGroup = ! 创建新的 WebGPU 对象(this,
GPUBindGroup, descriptor)。 -
在 this 的 设备时间线上执行 初始化步骤。
-
返回 bindGroup。
设备时间线 初始化步骤:-
令 limits = this.
[[device]].[[limits]]。 -
若下列任一条件不满足,则生成校验错误,使 bindGroup 无效并返回。
对于 descriptor.
entries中的每个GPUBindGroupEntrybindingDescriptor:-
令 resource = bindingDescriptor.
resource。 -
在 descriptor.
layout.entries中,存在且仅存在一个GPUBindGroupLayoutEntrylayoutBinding 满足 layoutBinding.binding等于 bindingDescriptor.binding。 -
若 layoutBinding 的定义binding member 为:
sampler-
-
resource 是
GPUSampler。 -
resource 可与 this 配合使用。
-
若 layoutBinding.
sampler.type为:"filtering"-
resource.
[[isComparison]]为false。 "non-filtering"-
resource.
[[isFiltering]]为false,且 resource.[[isComparison]]为false。 "comparison"-
resource.
[[isComparison]]为true。
-
texture-
-
resource 是
GPUTexture或GPUTextureView。 -
resource 可与 this 配合使用。
-
令 textureView = get as texture view(resource)。
-
令 texture = textureView.
[[texture]]。 -
layoutBinding.
texture.viewDimension等于 textureView 的dimension。 -
layoutBinding.
texture.sampleType与 textureView 的format兼容。 -
textureView.
[[descriptor]].usage包含TEXTURE_BINDING。 -
如果 layoutBinding.
texture.multisampled为true,则 texture 的sampleCount大于 1,否则为 1。
-
storageTexture-
-
resource 是
GPUTexture或GPUTextureView。 -
resource 可与 this 配合使用。
-
令 storageTextureView = get as texture view(resource)。
-
令 texture = storageTextureView.
[[texture]]。 -
layoutBinding.
storageTexture.viewDimension等于 storageTextureView 的dimension。 -
layoutBinding.
storageTexture.format等于 storageTextureView.[[descriptor]].format。 -
storageTextureView.
[[descriptor]].usage包含STORAGE_BINDING。 -
storageTextureView.
[[descriptor]].mipLevelCount必须为 1。
-
buffer-
-
resource 是
GPUBuffer或GPUBufferBinding。 -
令 bufferBinding = get as buffer binding(resource)。
-
bufferBinding.
buffer可与 this 配合使用。 -
由 bufferBinding.
offset和 bufferBinding.size决定的绑定部分位于缓冲区内部且大小非零。 -
effective buffer binding size(bufferBinding) ≥ layoutBinding.
buffer.minBindingSize。 -
若 layoutBinding.
buffer.type为:"uniform"-
-
effective buffer binding size(bufferBinding) ≤ limits.
maxUniformBufferBindingSize。 -
bufferBinding.
offset是 limits.minUniformBufferOffsetAlignment的倍数。
"storage"或"read-only-storage"-
-
effective buffer binding size(bufferBinding) ≤ limits.
maxStorageBufferBindingSize。 -
effective buffer binding size(bufferBinding) 是 4 的倍数。
-
bufferBinding.
offset是 limits.minStorageBufferOffsetAlignment的倍数。
-
externalTexture-
-
resource 是
GPUExternalTexture、GPUTexture或GPUTextureView。 -
resource 可与 this 配合使用。
-
若 resource 为:
GPUTexture或GPUTextureView-
-
令 view = get as texture view(resource)。
-
view.
[[descriptor]].usage必须包含TEXTURE_BINDING。 -
view.
[[descriptor]].dimension必须为"2d"。 -
view.
[[descriptor]].mipLevelCount必须为 1。 -
view.
[[descriptor]].format必须为"rgba8unorm"、"bgra8unorm"或"rgba16float"。 -
view.
[[texture]].sampleCount必须为 1。
-
-
-
-
令 bindGroup.
[[layout]]= descriptor.layout。 -
令 bindGroup.
[[entries]]= descriptor.entries。 -
令 bindGroup.
[[usedResources]]= {}。 -
对 descriptor.
entries中的每个GPUBindGroupEntrybindingDescriptor:-
令 internalUsage 为 layoutBinding 的绑定用法。
-
resource 所见的每个子资源,都以 internalUsage 形式添加到
[[usedResources]]。 -
若 layoutBinding 的 binding member 为
buffer且 layoutBinding.buffer.minBindingSize为0,则 bindingDescriptor.[[prevalidatedSize]]设为false,否则设为true。
-
-
参数:
-
GPUBindingResourceresource
返回: GPUTextureView
-
断言 resource 必须为
GPUTexture或GPUTextureView。 -
如果 resource 为:
GPUTexture-
-
返回 resource.
createView()。
-
GPUTextureView-
-
返回 resource。
-
参数:
-
GPUBindingResourceresource
返回: GPUBufferBinding
-
断言 resource 必须为
GPUBuffer或GPUBufferBinding。 -
如果 resource 为:
GPUBuffer-
-
令 bufferBinding 为新的
GPUBufferBinding。 -
设置 bufferBinding.
buffer为 resource。 -
返回 bufferBinding。
-
GPUBufferBinding-
-
返回 resource。
-
GPUBufferBinding
对象 a 和 b 被认为是 缓冲区绑定别名(buffer-binding-aliasing),当且仅当满足以下全部条件:
注意:进行该计算时,任何动态偏移已应用到范围上。
8.3. GPUPipelineLayout
GPUPipelineLayout
定义了在命令编码期间通过 setBindGroup() 设置的所有 GPUBindGroup
对象的资源,与通过 GPURenderCommandsMixin.setPipeline
或 GPUComputePassEncoder.setPipeline
设置的管线着色器之间的映射关系。
资源的完整绑定地址可以定义为以下三元组:
-
资源可见的着色器阶段掩码
-
绑定组索引(bind group index)
-
绑定号(binding number)
该地址的各分量也可视为管线的绑定空间。一个 GPUBindGroup
(与相应的 GPUBindGroupLayout
一起)
覆盖了某个固定绑定组索引的空间。其包含的绑定需要是该绑定组索引上着色器所用资源的超集。
[Exposed =(Window ,Worker ),SecureContext ]interface GPUPipelineLayout { };GPUPipelineLayout includes GPUObjectBase ;
GPUPipelineLayout
具有以下设备时间线属性:
[[bindGroupLayouts]],类型为 list<GPUBindGroupLayout>, 只读-
在创建时通过
GPUPipelineLayoutDescriptor.bindGroupLayouts提供的GPUBindGroupLayout对象列表。
注意:为多个 GPURenderPipeline
或 GPUComputePipeline
管线复用同一个 GPUPipelineLayout
可确保在切换这些管线时,用户代理无需在内部重新绑定资源。
GPUComputePipeline
对象 X 是用 GPUPipelineLayout.bindGroupLayouts
A、B、C 创建的。GPUComputePipeline
对象 Y 是用 GPUPipelineLayout.bindGroupLayouts
A、D、C 创建的。假设命令编码顺序有两次 dispatch:
-
setBindGroup(0, ...)
-
setBindGroup(1, ...)
-
setBindGroup(2, ...)
-
setPipeline(X) -
setBindGroup(1, ...)
-
setPipeline(Y)
在该场景中,即使 GPUPipelineLayout.bindGroupLayouts
索引 2 处的 GPUBindGroupLayout
或槽 2 处的 GPUBindGroup
都未变化,用户代理仍需为第二次 dispatch 重新绑定组槽 2。
注意: GPUPipelineLayout
的建议用法是将最常用且最少变动的绑定组放在布局“底部”,即绑定组槽号较低的位置(如 0 或 1)。绑定组在绘制调用间变动越频繁,其索引应越高。该通用建议有助于用户代理最小化绘制调用间的状态切换,从而降低 CPU
开销。
8.3.1. 管线布局创建
GPUPipelineLayout
通过 GPUDevice.createPipelineLayout()
创建。
dictionary :GPUPipelineLayoutDescriptor GPUObjectDescriptorBase {required sequence <GPUBindGroupLayout ?>bindGroupLayouts ; };
GPUPipelineLayoutDescriptor
字典定义了管线所用的所有 GPUBindGroupLayout,包含以下成员:
bindGroupLayouts,类型为sequence<GPUBindGroupLayout?>-
管线将使用的可选
GPUBindGroupLayout列表。每个元素对应GPUShaderModule中的 @group 属性,第 N 个元素对应@group(N)。
createPipelineLayout(descriptor)-
创建一个
GPUPipelineLayout。调用对象:GPUDevicethis。参数:
GPUDevice.createPipelineLayout(descriptor) 方法参数。 参数 类型 可为 null 可选 描述 descriptorGPUPipelineLayoutDescriptor✘ ✘ 要创建的 GPUPipelineLayout的描述信息。内容时间线步骤:
-
令 pl = ! 创建新的 WebGPU 对象(this,
GPUPipelineLayout, descriptor)。 -
在 this 的 设备时间线上执行 初始化步骤。
-
返回 pl。
设备时间线 初始化步骤:-
令 limits = this.
[[device]].[[limits]]。 -
令 bindGroupLayouts 为一个包含
null的GPUBindGroupLayout列表, 大小等于 limits.maxBindGroups。 -
对 descriptor.
bindGroupLayouts中每个索引 i 的 bindGroupLayout:-
如果 bindGroupLayout 非
null且 bindGroupLayout.[[descriptor]].entries非空:-
设置 bindGroupLayouts[i] 为 bindGroupLayout。
-
-
-
令 allEntries 为所有非
nullbgl 的 bgl.[[descriptor]].entries拼接后的结果。 -
若下列任一条件不满足,则生成校验错误,使 pl 无效并返回。
-
bindGroupLayouts 中每个非
null的GPUBindGroupLayout必须可与 this 配合使用,且其[[exclusivePipeline]]为null。 -
descriptor.
bindGroupLayouts的大小必须小于等于 limits.maxBindGroups。 -
allEntries 不能超出绑定槽限制 limits。
-
-
设置 pl.
[[bindGroupLayouts]]为 bindGroupLayouts。
-
注意: 若两个 GPUPipelineLayout
的内部 [[bindGroupLayouts]]
序列中包含的每个 GPUBindGroupLayout
对象两两 组等价,则它们对任意用途都是等价的。
8.4. 示例
GPUBindGroupLayout
,描述具有 uniform buffer、纹理和采样器的绑定。
然后使用该 GPUBindGroupLayout
创建 GPUBindGroup
和 GPUPipelineLayout。
const bindGroupLayout= gpuDevice. createBindGroupLayout({ entries: [{ binding: 0 , visibility: GPUShaderStage. VERTEX| GPUShaderStage. FRAGMENT, buffer: {} }, { binding: 1 , visibility: GPUShaderStage. FRAGMENT, texture: {} }, { binding: 2 , visibility: GPUShaderStage. FRAGMENT, sampler: {} }] }); const bindGroup= gpuDevice. createBindGroup({ layout: bindGroupLayout, entries: [{ binding: 0 , resource: { buffer: buffer}, }, { binding: 1 , resource: texture}, { binding: 2 , resource: sampler}] }); const pipelineLayout= gpuDevice. createPipelineLayout({ bindGroupLayouts: [ bindGroupLayout] });
9. 着色器模块
9.1. GPUShaderModule
[Exposed =(Window ,Worker ),SecureContext ]interface GPUShaderModule {Promise <GPUCompilationInfo >getCompilationInfo (); };GPUShaderModule includes GPUObjectBase ;
GPUShaderModule
是对内部着色器模块对象的引用。
9.1.1. 着色器模块创建
dictionary :GPUShaderModuleDescriptor GPUObjectDescriptorBase {required USVString code ;sequence <GPUShaderModuleCompilationHint >compilationHints = []; };
code,类型为 USVString-
该着色器模块的 WGSL 源代码。
compilationHints,类型为 sequence<GPUShaderModuleCompilationHint>,默认值为[]-
GPUShaderModuleCompilationHint的列表。应用程序提供的任何 hint 应当 包含有关将来会从该入口创建管线的某个 entry point 的信息。
实现 应当 使用
GPUShaderModuleCompilationHint中的任何信息,尽可能在createShaderModule()内完成编译。除了类型检查外,这些 hint 不会以任何方式被校验。
注意:在compilationHints中提供信息不会产生除性能以外的可观察效果。为从未创建的管线提供 hint 可能对性能有害。由于单个着色器模块可以包含多个入口点,并且可以从一个着色器模块创建多个管线,因此实现只在
createShaderModule()时尽可能多地编译会更高效,而不是在多次createComputePipeline()或createRenderPipeline()时多次编译。hint 只应用于其明确命名的入口点。与
GPUProgrammableStage.entryPoint不同,即使模块中只有一个入口点,这里也没有默认值。注意: hint 不会以可观察方式被校验,但用户代理 可以 向开发者(比如在浏览器开发者工具)展示可识别错误(如未知入口点名或不兼容的管线布局)。
createShaderModule(descriptor)-
创建一个
GPUShaderModule。调用对象:GPUDevicethis。参数:
GPUDevice.createShaderModule(descriptor) 方法参数。 参数 类型 可为 null 可选 描述 descriptorGPUShaderModuleDescriptor✘ ✘ 要创建的 GPUShaderModule的描述信息。返回:
GPUShaderModule内容时间线步骤:
-
令 sm = ! 创建新的 WebGPU 对象(this,
GPUShaderModule, descriptor)。 -
在 this 的 设备时间线上执行 初始化步骤。
-
返回 sm。
设备时间线 初始化步骤:-
令 error 为使用 WGSL 源 descriptor.
code创建着色器模块时产生的任何错误,若无错误则为null。 -
若下列任一要求不满足,则生成校验错误,使 sm 无效并返回。
-
this 不能丢失。
-
error 不能为 shader-creation program error。
-
对 descriptor.
code中每个enable扩展,对应的GPUFeatureName必须被启用(见 特性索引)。
注意: 未分类错误不会在着色器模块创建时出现。实现如果在着色器模块创建阶段检测到此类错误,必须表现为着色器模块有效,并将错误推迟到管线创建阶段再暴露。
-
注意:用户代理不应在此处校验错误的message文本中包含详细的编译器错误消息或着色器代码文本:这些详情可通过getCompilationInfo()访问。用户代理应为开发者提供易读、格式化的错误详情(比如在浏览器开发者工具中以可展开警告展示完整着色器源码)。由于生产环境中着色器编译错误应该很罕见,用户代理可以选择无论错误处理方式如何(GPU 错误作用域 或
uncapturederror事件处理器),都向开发者展示这些错误,例如以可展开警告形式。 如果不这样做,应当提供并记录其他方式让开发者访问这些可读错误详情,比如添加一个复选框总是显示错误,或在控制台记录GPUCompilationInfo对象时显示可读详情。 -
GPUShaderModule:
// 一个简单的 vertex 和 fragment 着色器对,将用红色填充视口。 const shaderSource= ` var<private> pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>( vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0)); @vertex fn vertexMain(@builtin(vertex_index) vertexIndex : u32) -> @builtin(position) vec4<f32> { return vec4(pos[vertexIndex], 1.0, 1.0); } @fragment fn fragmentMain() -> @location(0) vec4<f32> { return vec4(1.0, 0.0, 0.0, 1.0); } ` ; const shaderModule= gpuDevice. createShaderModule({ code: shaderSource, });
9.1.1.1. 着色器模块编译提示
着色器模块编译提示是可选的附加信息,用于指明某个 GPUShaderModule
的入口点将来如何被使用。对于某些实现,这些信息可以帮助更早完成着色器模块编译,从而提升性能。
dictionary {GPUShaderModuleCompilationHint required USVString ; (entryPoint GPUPipelineLayout or GPUAutoLayoutMode )layout ; };
layout,类型为(GPUPipelineLayout or GPUAutoLayoutMode)-
一个
GPUPipelineLayout,表示GPUShaderModule在后续createComputePipeline()或createRenderPipeline()调用中可能会与之配合使用。 如果设置为"auto",则会为该 hint 关联的入口点使用默认管线布局。
createShaderModule()
和 createComputePipeline()
/
createRenderPipeline()
之间提供一致的信息。
如果应用在调用 createShaderModule()
时无法提供 hint 信息,通常不应因此推迟调用 createShaderModule(),而应在
compilationHints
序列或单个 GPUShaderModuleCompilationHint
成员中省略未知信息。省略该信息可能导致编译被延迟到 createComputePipeline()
/ createRenderPipeline()。
如果作者无法确保传给 createShaderModule()
的 hint 信息会与后续 createComputePipeline()
/
createRenderPipeline()
使用的信息一致,则应避免向 createShaderModule()
提供该信息,因为不匹配的 hint 可能导致不必要的编译发生。
9.1.2. 着色器模块编译信息
enum {GPUCompilationMessageType ,"error" ,"warning" , }; ["info" Exposed =(Window ,Worker ),Serializable ,SecureContext ]interface {GPUCompilationMessage readonly attribute DOMString message ;readonly attribute GPUCompilationMessageType type ;readonly attribute unsigned long long lineNum ;readonly attribute unsigned long long linePos ;readonly attribute unsigned long long offset ;readonly attribute unsigned long long length ; }; [Exposed =(Window ,Worker ),Serializable ,SecureContext ]interface {GPUCompilationInfo readonly attribute FrozenArray <GPUCompilationMessage >; };messages
GPUCompilationMessage
是 GPUShaderModule
编译器生成的信息、警告或错误消息。这些消息面向开发者、可读性强,用于帮助诊断着色器 code
中的问题。每条消息可能对应着色器代码中的某个具体位置、某段子串,或根本不对应代码中的任何具体位置。
GPUCompilationMessage
具有以下属性:
message,类型为 DOMString,只读-
该编译消息的人类可读、可本地化文本。
注意:
message应遵循字符串语言和方向元信息最佳实践,包括未来可能出现的相关标准。编者注: 截至目前,还没有兼容并与传统 API 保持一致的语言/方向推荐方案,未来如有应正式采纳。
type,类型为 GPUCompilationMessageType,只读-
消息的严重级别。
如果
type为"error", 则该消息对应 shader-creation error。 lineNum,类型为 unsigned long long,只读-
消息对应的着色器
code的行号。值为 1 起始,1表示第一行。行由换行符分隔。如果
message对应某个子串,则此处为子串起始行。若消息不对应代码中任何具体点,则为0。 linePos,类型为 unsigned long long,只读-
从第
lineNum行起始到消息对应点或子串起始的 UTF-16 单元偏移量。值为 1 起始,linePos为1表示该行第一个单元。如对应子串,则指向子串第一个 UTF-16 单元。若消息不对应代码中任何具体点,则为
0。 offset,类型为 unsigned long long,只读-
从着色器
code起始到消息对应点或子串起始的 UTF-16 单元偏移量。必须与lineNum和linePos指向同一位置。若消息不对应代码中任何具体点,则为0。 length,类型为 unsigned long long,只读-
消息对应子串的 UTF-16 单元数量。若消息不对应子串则为
0。
注意: GPUCompilationMessage.lineNum
和 GPUCompilationMessage.linePos
都是从 1 起始,便于与大多数文本编辑器的行列号一致。
注意: GPUCompilationMessage.offset
和 GPUCompilationMessage.length
可直接传给 substr(),取出消息对应的着色器 code
子串。
getCompilationInfo()-
返回
GPUShaderModule编译期间的所有消息。消息的位置、顺序和内容均为实现自定义,不保证以
lineNum排序。设备时间线 同步步骤:-
令 event 在 this 的着色器模块创建完成(无论成功与否)时发生。
-
监听时间线事件 event 于 this.
[[device]], 后续步骤在 contentTimeline 上执行。
内容时间线步骤:-
令 info 为新的
GPUCompilationInfo。 -
令 messages 为 this 的着色器模块创建期间产生的所有错误、警告或信息消息(如设备丢失则为
[])。 -
对于 messages 中每条 message:
-
令 m 为新的
GPUCompilationMessage。 -
设置 m.
message为 message 的文本。
-
-
resolve promise 为 info。
-
10. 管线
管线(pipeline),无论是 GPUComputePipeline
还是 GPURenderPipeline,
都代表由 GPU 硬件、驱动和用户代理共同完成的完整功能流程:处理绑定和顶点缓冲区等输入数据,并产生输出(比如输出渲染目标的颜色)。
结构上,管线由一系列可编程阶段(着色器)和固定功能状态(如混合模式)组成。
注意: 在内部,具体实现平台可能会把部分固定功能状态转换成着色器代码,并与用户提供的着色器链接。这种链接是管线对象必须整体创建的原因之一。
这种组合状态作为单个对象创建(GPUComputePipeline
或 GPURenderPipeline),
并可通过单条指令切换(分别为 GPUComputePassEncoder.setPipeline()
或 GPURenderCommandsMixin.setPipeline())。
创建管线有两种方式:
- 同步(即时)管线创建
-
createComputePipeline()和createRenderPipeline()返回可立即在 pass encoder 中使用的管线对象。注意: 返回的是句柄对象,但实际管线创建并非同步。如果管线创建耗时较长,可能在从创建到首次
setPipeline()使用、finish()、或submit()之间的某一处导致 设备时间线 阻塞,具体点未规定。 - 异步管线创建
-
createComputePipelineAsync()和createRenderPipelineAsync()返回Promise,管线创建完成时解析为管线对象。失败时,
Promise拒绝并返回GPUPipelineError。
GPUPipelineError 描述管线创建失败。
[Exposed =(Window ,Worker ),SecureContext ,Serializable ]interface GPUPipelineError :DOMException {constructor (optional DOMString message = "",GPUPipelineErrorInit options );readonly attribute GPUPipelineErrorReason reason ; };dictionary {GPUPipelineErrorInit required GPUPipelineErrorReason ; };reason enum GPUPipelineErrorReason {"validation" ,"internal" , };
GPUPipelineError
构造函数:
constructor()-
参数:
GPUPipelineError.constructor() 方法参数。 参数 类型 可为 null 可选 描述 messageDOMString✘ ✔ 基础 DOMException的错误消息。optionsGPUPipelineErrorInit✘ ✘ 特定于 GPUPipelineError的选项。内容时间线步骤:
GPUPipelineError
具有以下属性:
reason,类型为 GPUPipelineErrorReason,只读-
只读slot-backed 属性,暴露管线创建中遇到的错误类型,为
GPUPipelineErrorReason:
-
按
DOMException的序列化步骤处理 value 和 serialized。
-
按
DOMException的反序列化步骤处理 value 和 serialized。
10.1. 基础管线
enum {GPUAutoLayoutMode , };"auto" dictionary :GPUPipelineDescriptorBase GPUObjectDescriptorBase {required (GPUPipelineLayout or GPUAutoLayoutMode )layout ; };
layout,类型为(GPUPipelineLayout or GPUAutoLayoutMode)-
本管线的
GPUPipelineLayout, 或"auto"以自动生成管线布局。注意: 若使用
"auto", 则此管线无法与其它管线共享GPUBindGroup。
interface mixin { [GPUPipelineBase NewObject ]GPUBindGroupLayout getBindGroupLayout (unsigned long index ); };
GPUPipelineBase
具有以下设备时间线属性:
[[layout]],类型为GPUPipelineLayout-
定义可与
this配合使用的资源布局。
GPUPipelineBase
具有以下方法:
getBindGroupLayout(index)-
获取与
GPUPipelineBase在index处的GPUBindGroupLayout兼容的GPUBindGroupLayout。调用对象:GPUPipelineBasethis参数:
GPUPipelineBase.getBindGroupLayout(index) 方法参数。 参数 类型 可为 null 可选 描述 indexunsigned long✘ ✘ 管线布局 [[bindGroupLayouts]]序列的索引。内容时间线步骤:
-
令 layout 为新的
GPUBindGroupLayout对象。 -
在 this 的 设备时间线上执行 初始化步骤。
-
返回 layout。
设备时间线 初始化步骤:-
令 limits = this.
[[device]].[[limits]]。 -
若下列任一条件不满足,则生成校验错误,使 layout 无效并返回。
-
this 必须有效。
-
index < limits.
maxBindGroups。
-
-
初始化 layout 使其为 this.
[[layout]].[[bindGroupLayouts]][index] 的副本。注意:
GPUBindGroupLayout永远是按值使用,不按引用,因此等价于返回同一个内部对象并创建新WebGPU 接口。每次都返回新GPUBindGroupLayoutWebGPU 接口,以避免 内容时间线与 设备时间线之间的往返。
-
10.1.1. 默认管线布局
如果 GPUPipelineBase
对象在创建时 layout
设置为 "auto",则会自动创建并使用一个默认布局。
注意: 默认布局为简易管线提供便利,大多数场景推荐显式布局。由默认布局创建的绑定组无法与其他管线共享,且默认布局结构在着色器变更时可能发生变化,导致绑定组创建异常。
为 GPUPipelineBase
pipeline 创建默认管线布局的
设备时间线步骤如下:
-
令 groupCount = 0。
-
令 groupDescs 为 device.
[[limits]].maxBindGroups个新的GPUBindGroupLayoutDescriptor对象序列。 -
对每个 groupDesc in groupDescs:
-
对创建 pipeline 时描述中的每个
GPUProgrammableStagestageDesc:-
令 shaderStage 为 stageDesc 在 pipeline 中对应的
GPUShaderStageFlags。 -
令 entryPoint = get the entry point(shaderStage, stageDesc)。断言 entryPoint 非
null。 -
对 entryPoint 静态使用的每个资源 resource:
-
令 group 为 resource 的 "group" 标注。
-
令 binding 为 resource 的 "binding" 标注。
-
令 entry 为新的
GPUBindGroupLayoutEntry。 -
设置 entry.
binding为 binding。 -
设置 entry.
visibility为 shaderStage。 -
若 resource 为采样器绑定:
-
令 samplerLayout 为新的
GPUSamplerBindingLayout。 -
设置 entry.
sampler为 samplerLayout。
-
-
若 resource 为比较采样器绑定:
-
令 samplerLayout 为新的
GPUSamplerBindingLayout。 -
设置 samplerLayout.
type为"comparison"。 -
设置 entry.
sampler为 samplerLayout。
-
-
若 resource 为缓冲区绑定:
-
令 bufferLayout 为新的
GPUBufferBindingLayout。 -
设置 bufferLayout.
minBindingSize为 resource 的最小缓冲绑定大小。 -
如果 resource 为只读存储缓冲区:
-
设置 bufferLayout.
type为"read-only-storage"。
-
-
如果 resource 为存储缓冲区:
-
设置 entry.
buffer为 bufferLayout。
-
-
若 resource 为采样纹理绑定:
-
令 textureLayout 为新的
GPUTextureBindingLayout。 -
如果 resource 为深度纹理绑定:
-
设置 textureLayout.
sampleType为"depth"。
否则,根据 resource 采样类型:
f32且存在对 resource 的静态使用,且在带采样器的纹理内建函数调用中-
设置 textureLayout.
sampleType为"float"。 f32否则-
设置 textureLayout.
sampleType为"unfilterable-float"。 i32-
设置 textureLayout.
sampleType为"sint"。 u32-
设置 textureLayout.
sampleType为"uint"。
-
-
设置 textureLayout.
viewDimension为 resource 的维度。 -
若 resource 为多采样纹理:
-
设置 textureLayout.
multisampled为true。
-
-
设置 entry.
texture为 textureLayout。
-
-
若 resource 为存储纹理绑定:
-
令 storageTextureLayout 为新的
GPUStorageTextureBindingLayout。 -
设置 storageTextureLayout.
format为 resource 的格式。 -
设置 storageTextureLayout.
viewDimension为 resource 的维度。 -
若访问模式为:
read-
设置 textureLayout.
access为"read-only"。 write-
设置 textureLayout.
access为"write-only"。 read_write-
设置 textureLayout.
access为"read-write"。
-
设置 entry.
storageTexture为 storageTextureLayout。
-
-
设置 groupCount = max(groupCount, group + 1)。
-
若 groupDescs[group] 已存在
binding等于 binding 的条目 previousEntry:-
若 entry 的
visibility不同于 previousEntry:-
将 entry.
visibility的比特位并入 previousEntry.visibility。
-
-
若 resource 为缓冲区绑定且 entry 的
buffer.minBindingSize大于 previousEntry:-
设置 previousEntry.
buffer.minBindingSize为 entry.buffer.minBindingSize。
-
-
若 resource 为采样纹理绑定且 entry 的
texture.sampleType与 previousEntry 不同,且二者均为"float"或"unfilterable-float":-
设置 previousEntry.
texture.sampleType为"float"。
-
-
如 entry 与 previousEntry 其他属性不同:
-
返回
null(导致管线创建失败)。
-
-
若 resource 为存储纹理绑定,entry.storageTexture.
access为"read-write",previousEntry.storageTexture.access为"write-only",且格式兼容,见 § 26.1.1 普通色彩格式表:-
设置 previousEntry.storageTexture.
access为"read-write"。
-
-
-
否则
-
将 entry 添加到 groupDescs[group]。
-
-
-
-
令 groupLayouts 为新 列表。
-
对 i 从 0 到 groupCount - 1:
-
令 groupDesc = groupDescs[i]。
-
令 bindGroupLayout = device.
createBindGroupLayout()(groupDesc)。 -
设置 bindGroupLayout.
[[exclusivePipeline]]为 pipeline。 -
将 bindGroupLayout 添加到 groupLayouts。
-
-
令 desc 为新的
GPUPipelineLayoutDescriptor。 -
设置 desc.
bindGroupLayouts为 groupLayouts。 -
返回 device.
createPipelineLayout()(desc)。
10.1.2. GPUProgrammableStage
GPUProgrammableStage
描述了用户提供的 GPUShaderModule
中控制 管线某个可编程阶段的入口点。
入口点名称遵循 WGSL 标识符对比的规则。
dictionary GPUProgrammableStage {required GPUShaderModule module ;USVString entryPoint ;record <USVString ,GPUPipelineConstantValue >constants = {}; };typedef double GPUPipelineConstantValue ; // 可表示 WGSL 的 bool、f32、i32、u32、以及启用时的 f16。
GPUProgrammableStage
具有以下成员:
module,类型为 GPUShaderModule-
包含本可编程阶段将要执行代码的
GPUShaderModule。 entryPoint,类型为 USVString-
本阶段执行时将使用的
module中的函数名。注意: 虽然
entryPoint不是必填项,消费GPUProgrammableStage的方法必须使用“获取入口点”算法确定实际入口点。 constants,类型为 record<USVString, GPUPipelineConstantValue>,默认值为{}-
指定着色器模块
module中 可被管线重写(pipeline-overridable) 常量的值。每个此类 可重写常量都唯一由一个 可重写常量标识符字符串标识, 代表其声明指定的 管线常量 ID,否则为常量名称。
每个键值对的 key 必须等于对应常量的 标识符字符串,其对比规则见WGSL 标识符对比。管线执行时,该常量将取指定值。
值类型为
GPUPipelineConstantValue,即double。会转换为常量的 WGSL 类型(bool/i32/u32/f32/f16)。转换失败会生成校验错误。WGSL 中定义的管线可重写常量:@id ( 0 ) override has_point_light : bool= true ; // 算法控制。 @id ( 1200 ) override specular_param : f32= 2.3 ; // 数值控制。 @id ( 1300 ) override gain : f32; // 必须重写。 override width : f32= 0.0 ; // 通过 API 使用名字 "width" 指定 // override depth : f32; // 通过 API 使用名字 "depth" 指定,必须重写。 override height = 2 * depth ; // 默认值依赖另一个可重写常量。 对应的 JavaScript 代码,仅传递必须重写(无默认值)的常量:
{ // ... constants: { 1300 : 2.0 , // "gain" depth: - 1 , // "depth" } } 对应的 JavaScript 代码,重写所有常量:
{ // ... constants: { 0 : false , // "has_point_light" 1200 : 3.0 , // "specular_param" 1300 : 2.0 , // "gain" width: 20 , // "width" depth: - 1 , // "depth" height: 15 , // "height" } }
GPUShaderStage
stage,
GPUProgrammableStage
descriptor),请执行如下设备时间线步骤:
-
如果 descriptor.
entryPoint已提供:-
如果 descriptor.
module包含名称与 descriptor.entryPoint相等且着色器阶段等于 stage 的入口点,则返回该入口点。否则,返回
null。
否则:
-
如果 descriptor.
module中着色器阶段等于 stage 的入口点恰好有一个,则返回该入口点。否则,返回
null。
-
参数:
-
GPUShaderStagestage -
GPUProgrammableStagedescriptor -
GPUPipelineLayoutlayout -
GPUDevicedevice
以下所有步骤中的要求都必须满足。若有任一不满足,则返回 false;全部满足则返回 true。
-
令 entryPoint = 获取入口点(stage, descriptor)。
-
entryPoint 必须不为
null。 -
对 entryPoint 静态使用的每个 binding:
-
验证着色器绑定(validating shader binding)(binding, layout) 必须返回
true。
-
-
对 entryPoint 所在着色器阶段所有函数中的每个纹理内建函数调用,若同时使用了 sampled texture 或 depth texture 类型的 textureBinding 以及类型为
sampler(排除sampler_comparison)的 samplerBinding:-
令 texture 为 textureBinding 对应的
GPUBindGroupLayoutEntry。 -
令 sampler 为 samplerBinding 对应的
GPUBindGroupLayoutEntry。 -
若 sampler.
type为"filtering", 则 texture.sampleType必须为"float"。
注意:
"comparison"采样器只能用于"depth"纹理,因为只有该类型能绑定到 WGSLtexture_depth_*绑定点。 -
-
对 descriptor.
constants的每个 key → value:-
key 必须等于着色器模块 descriptor.
module中某个 可重写常量(pipeline-overridable) 的 标识符字符串(判断规则见 WGSL 标识符对比)。管线可重写常量无需被 entryPoint 静态使用。令该常量类型为 T。 -
将 IDL 值 value 转换为 WGSL 类型 T 时不得抛出
TypeError。
-
-
对 entryPoint 静态使用的每个管线可重写常量 key:
参数:
-
着色器绑定声明 variable,为从着色器模块反射得到的模块作用域变量声明
-
GPUPipelineLayoutlayout
令 bindGroup 为绑定组索引,bindIndex 为绑定索引,均来自着色器绑定声明 variable。
当且仅当下列所有条件满足时,返回 true:
-
layout.
[[bindGroupLayouts]][bindGroup] 包含 一个GPUBindGroupLayoutEntryentry,满足 entry.binding== bindIndex。 -
若 entry 的binding member为:
buffer-
"uniform"-
variable 声明于地址空间
uniform。 "storage"-
variable 声明于地址空间
storage且访问模式为read_write。 "read-only-storage"-
variable 声明于地址空间
storage且访问模式为read。
若 entry.
buffer.minBindingSize非0, 则其必须不少于着色器中对应缓冲绑定变量的最小缓冲绑定大小。 sampler-
"filtering"或"non-filtering"-
variable 类型为
sampler。 "comparison"-
variable 类型为
sampler_comparison。
texture-
当且仅当 entry.
texture.multisampled为true时,variable 类型为texture_multisampled_2d<T>或texture_depth_multisampled_2d<T>。若 entry.
texture.sampleType为:"float","unfilterable-float","sint"或"uint"-
variable 类型为:
-
texture_1d<T> -
texture_2d<T> -
texture_2d_array<T> -
texture_cube<T> -
texture_cube_array<T> -
texture_3d<T> -
texture_multisampled_2d<T>
若 entry.
texture.sampleType为:"float"或"unfilterable-float"-
采样类型
T为f32。 "sint"-
采样类型
T为i32。 "uint"-
采样类型
T为u32。
-
"depth"-
variable 类型为下列之一:
-
texture_2d<T> -
texture_2d_array<T> -
texture_cube<T> -
texture_cube_array<T> -
texture_multisampled_2d<T> -
texture_depth_2d -
texture_depth_2d_array -
texture_depth_cube -
texture_depth_cube_array -
texture_depth_multisampled_2d
其中采样类型
T为f32。 -
若 entry.
texture.viewDimension为:"1d"-
variable 类型为
texture_1d<T>。 "2d"-
variable 类型为
texture_2d<T>或texture_multisampled_2d<T>。 "2d-array"-
variable 类型为
texture_2d_array<T>。 "cube"-
variable 类型为
texture_cube<T>。 "cube-array"-
variable 类型为
texture_cube_array<T>。 "3d"-
variable 类型为
texture_3d<T>。
storageTexture-
若 entry.
storageTexture.viewDimension为:"1d"-
variable 类型为
texture_storage_1d<T, A>。 "2d"-
variable 类型为
texture_storage_2d<T, A>。 "2d-array"-
variable 类型为
texture_storage_2d_array<T, A>。 "3d"-
variable 类型为
texture_storage_3d<T, A>。
若 entry.
storageTexture.access为:"write-only"-
访问模式
A为write。 "read-only"-
访问模式
A为read。 "read-write"-
访问模式
A为read_write或write。
纹素格式
T必须等于 entry.storageTexture.format。
-
令 T 为 var 的存储类型(store type)。
-
若 T 是运行时尺寸数组,或包含运行时尺寸数组,则将该
array<E>替换为array<E, 1>。注意: 这样确保至少有一个元素内存,允许数组索引被限制在实际长度范围内,从而保证内存访问安全。
-
返回 SizeOf(T)。
注意: 强制此下限可确保通过缓冲变量的读写只会访问缓冲区界限内的内存。
10.2. GPUComputePipeline
GPUComputePipeline
是一种管线,控制计算着色器阶段,
并可用于 GPUComputePassEncoder。
计算输入和输出均通过绑定传递,受指定的 GPUPipelineLayout
控制。
输出对应类型为 "storage"
的 buffer
绑定,
以及类型为 "write-only"
或 "read-write"
的 storageTexture
绑定。
计算管线的阶段:
-
计算着色器(Compute shader)
[Exposed =(Window ,Worker ),SecureContext ]interface GPUComputePipeline { };GPUComputePipeline includes GPUObjectBase ;GPUComputePipeline includes GPUPipelineBase ;
10.2.1. 计算管线的创建
GPUComputePipelineDescriptor
描述了一个计算管线。更多细节参见§ 23.1 计算。
dictionary :GPUComputePipelineDescriptor GPUPipelineDescriptorBase {required GPUProgrammableStage compute ; };
GPUComputePipelineDescriptor
具有以下成员:
compute,类型为 GPUProgrammableStage-
描述该管线的计算着色器入口点。
createComputePipeline(descriptor)-
使用同步管线创建方式创建一个
GPUComputePipeline。调用对象:GPUDevicethis.参数:
GPUDevice.createComputePipeline(descriptor) 方法参数。 参数 类型 可为 null 可选 描述 descriptorGPUComputePipelineDescriptor✘ ✘ 要创建的 GPUComputePipeline的描述信息。内容时间线步骤:
-
令 pipeline = ! 创建新 WebGPU 对象(create a new WebGPU object)(this,
GPUComputePipeline, descriptor)。 -
在 this 的 设备时间线上执行 初始化步骤。
-
返回 pipeline。
设备时间线 初始化步骤:-
若 descriptor.
layout为"auto", 则令 layout 为 默认管线布局; 否则,layout = descriptor.layout。 -
以下所有步骤要求都必须满足。若有不满足,则生成校验错误,使 pipeline 无效并返回。
-
layout 必须可与 this 配合使用。
-
验证 GPUProgrammableStage(
COMPUTE, descriptor.compute, layout, this) 必须成功。 -
令 entryPoint = 获取入口点(
COMPUTE, descriptor.compute)。断言 entryPoint 不为
null。 -
令 workgroupStorageUsed 为 roundUp(16, SizeOf(T)) 的和, 其中 T 为 entryPoint 静态使用的所有地址空间为 "workgroup" 的变量类型。
workgroupStorageUsed 必须 ≤ device.limits.
maxComputeWorkgroupStorageSize。 -
entryPoint 必须每个工作组内使用的线程数 ≤ device.limits.
maxComputeInvocationsPerWorkgroup。 -
entryPoint 的
workgroup_size属性的每个分量 必须 ≤ [device.limits.maxComputeWorkgroupSizeX, device.limits.maxComputeWorkgroupSizeY, device.limits.maxComputeWorkgroupSizeZ]。
-
-
如实现管线创建过程中产生了任何管线创建未分类错误, 生成内部错误,使 pipeline 无效并返回。
注意: 即便实现已在着色器模块创建时检测到未分类错误,也会在此处抛出。
-
设置 pipeline.
[[layout]]= layout。
-
createComputePipelineAsync(descriptor)-
使用异步管线创建方式创建
GPUComputePipeline。 返回的Promise会在管线准备好可用时 resolve。若管线创建失败,返回的
Promise将 reject,并返回GPUPipelineError。 (此时不会向 device 派发GPUError。)注意: 建议尽可能使用本方法,可避免阻塞队列时间线的管线编译工作。
调用对象:GPUDevicethis.参数:
GPUDevice.createComputePipelineAsync(descriptor) 方法参数。 参数 类型 可为 null 可选 描述 descriptorGPUComputePipelineDescriptor✘ ✘ 要创建的 GPUComputePipeline的描述信息。返回:
Promise<GPUComputePipeline>内容时间线步骤:
设备时间线 初始化步骤:-
令 pipeline 为新建的
GPUComputePipeline, 类似于调用 this.createComputePipeline()时传入 descriptor,但将产生的错误捕获为 error,而不是派发到 device。 -
令 event 在 pipeline 的管线创建(无论成功与否)完成时发生。
-
监听时间线事件 event 于 this.
[[device]], 后续步骤于 设备时间线执行。
设备时间线步骤:-
-
在 contentTimeline 上执行下列步骤:
-
返回。
注意: 设备丢失不会产生错误。参见 § 22 错误与调试。
-
-
若 pipeline 无效 且 error 为 内部错误,在 contentTimeline 上执行下列步骤并返回:
内容时间线步骤:-
reject promise,返回
GPUPipelineError,reason为"internal"。
-
-
若 pipeline 无效 且 error 为 校验错误,在 contentTimeline 上执行下列步骤并返回:
内容时间线步骤:-
reject promise,返回
GPUPipelineError,reason为"validation"。
-
-
GPUComputePipeline:
const computePipeline= gpuDevice. createComputePipeline({ layout: pipelineLayout, compute: { module: computeShaderModule, entryPoint: 'computeMain' , } });
10.3. GPURenderPipeline
GPURenderPipeline
是一种管线,控制顶点和片元着色器阶段,并可用于 GPURenderPassEncoder
以及 GPURenderBundleEncoder。
渲染管线的输入包括:
-
绑定,根据指定的
GPUPipelineLayout -
顶点和索引缓冲区,由
GPUVertexState描述 -
颜色附件,由
GPUColorTargetState描述 -
(可选)深度-模板附件,由
GPUDepthStencilState描述
渲染管线的输出包括:
-
storageTexture绑定,其access为"write-only"或"read-write" -
颜色附件,由
GPUColorTargetState描述 -
(可选)深度-模板附件,由
GPUDepthStencilState描述
渲染管线包含以下渲染阶段(render stages):
-
顶点抓取,由
GPUVertexState.buffers控制 -
顶点着色器,由
GPUVertexState控制 -
图元组装,由
GPUPrimitiveState控制 -
光栅化,由
GPUPrimitiveState、GPUDepthStencilState、 以及GPUMultisampleState控制 -
片元着色器,由
GPUFragmentState控制 -
模板测试和操作,由
GPUDepthStencilState控制 -
深度测试与写入,由
GPUDepthStencilState控制 -
输出合并,由
GPUFragmentState.targets控制
[Exposed =(Window ,Worker ),SecureContext ]interface GPURenderPipeline { };GPURenderPipeline includes GPUObjectBase ;GPURenderPipeline includes GPUPipelineBase ;
GPURenderPipeline
具有以下设备时间线属性:
[[descriptor]],类型为GPURenderPipelineDescriptor, 只读-
描述此管线的
GPURenderPipelineDescriptor。所有可选字段均已定义。
[[writesDepth]],类型为boolean, 只读-
如果管线会写入深度/模板附件的深度分量,则为 true。
[[writesStencil]],类型为boolean, 只读-
如果管线会写入深度/模板附件的模板分量,则为 true。
10.3.1. 渲染管线的创建
GPURenderPipelineDescriptor
通过配置各渲染阶段描述一个渲染管线。详见 § 23.2 渲染。
dictionary :GPURenderPipelineDescriptor GPUPipelineDescriptorBase {required GPUVertexState vertex ;GPUPrimitiveState primitive = {};GPUDepthStencilState depthStencil ;GPUMultisampleState multisample = {};GPUFragmentState fragment ; };
GPURenderPipelineDescriptor
具有以下成员:
vertex,类型为 GPUVertexState-
描述管线的顶点着色器入口点和其输入缓冲区布局。
primitive,类型为 GPUPrimitiveState,默认值为{}-
描述管线的图元相关属性。
depthStencil,类型为 GPUDepthStencilState-
描述可选的深度-模板属性,包括测试、操作和偏移。
multisample,类型为 GPUMultisampleState,默认值为{}-
描述管线的多重采样属性。
fragment,类型为 GPUFragmentState-
描述管线的片元着色器入口点及其输出颜色。如未提供,则启用§ 23.2.8 无颜色输出模式。
createRenderPipeline(descriptor)-
使用同步管线创建方式创建
GPURenderPipeline。调用对象:GPUDevicethis.参数:
GPUDevice.createRenderPipeline(descriptor) 方法参数。 参数 类型 可为 null 可选 描述 descriptorGPURenderPipelineDescriptor✘ ✘ 要创建的 GPURenderPipeline描述信息。内容时间线步骤:
-
-
遍历 descriptor.
fragment.targets的所有非nullcolorState:-
? 校验纹理格式所需特性 用 colorState.
format和 this.[[device]]。
-
-
-
若 descriptor.
depthStencil已提供:-
? 校验纹理格式所需特性 用 descriptor.
depthStencil.format和 this.[[device]]。
-
-
令 pipeline = ! 创建新 WebGPU 对象(this,
GPURenderPipeline, descriptor)。 -
在 this 的 设备时间线上执行 初始化步骤。
-
返回 pipeline。
设备时间线 初始化步骤:-
若 descriptor.
layout为"auto", 则令 layout 为 默认管线布局, 否则 layout = descriptor.layout。 -
以下步骤要求全部必须满足。如有不满足,则生成校验错误、使 pipeline 无效并返回。
-
layout 必须可与 this 配合使用。
-
验证 GPURenderPipelineDescriptor(descriptor, layout, this) 必须成功。
-
令 vertexBufferCount 为 descriptor.
vertex.buffers中最后一个非 null 项的索引加 1,如无则为 0。 -
layout.
[[bindGroupLayouts]].size + vertexBufferCount 必须 ≤ this.[[device]].[[limits]].maxBindGroupsPlusVertexBuffers。
-
-
如实现管线创建过程中产生任何管线创建未分类错误, 生成内部错误、使 pipeline 无效并返回。
注意: 即使着色器模块创建时检测到未分类错误,错误也会在此处抛出。
-
设置 pipeline.
[[descriptor]]= descriptor。 -
设置 pipeline.
[[writesDepth]]= false。 -
设置 pipeline.
[[writesStencil]]= false。 -
令 depthStencil = descriptor.
depthStencil。 -
如 depthStencil 非 null:
-
如 depthStencil.
depthWriteEnabled已提供:-
设置 pipeline.
[[writesDepth]]= depthStencil.depthWriteEnabled。
-
-
如 depthStencil.
stencilWriteMask非 0:-
令 stencilFront = depthStencil.
stencilFront。 -
令 stencilBack = depthStencil.
stencilBack。 -
如 cullMode 不为
"front",且 stencilFront.passOp、 stencilFront.depthFailOp、 stencilFront.failOp任意一项非"keep":-
设置 pipeline.
[[writesStencil]]= true。
-
-
如 cullMode 不为
"back",且 stencilBack.passOp、 stencilBack.depthFailOp、 stencilBack.failOp任意一项非"keep":-
设置 pipeline.
[[writesStencil]]= true。
-
-
-
-
设置 pipeline.
[[layout]]= layout。
-
createRenderPipelineAsync(descriptor)-
使用异步管线创建方式创建
GPURenderPipeline。 返回的Promise会在管线准备好可用时 resolve。若管线创建失败,返回的
Promise将 reject,并返回GPUPipelineError。 (不会向 device 派发GPUError。)注意: 建议尽量使用本方法,可避免阻塞队列时间线的管线编译工作。
调用对象:GPUDevicethis.参数:
GPUDevice.createRenderPipelineAsync(descriptor) 方法参数。 参数 类型 可为 null 可选 描述 descriptorGPURenderPipelineDescriptor✘ ✘ 要创建的 GPURenderPipeline描述信息。返回:
Promise<GPURenderPipeline>内容时间线步骤:
设备时间线 初始化步骤:-
令 pipeline 为新建的
GPURenderPipeline, 类似于调用 this.createRenderPipeline()时传入 descriptor,但将产生的错误捕获为 error,而不是派发到 device。 -
令 event 在 pipeline 的管线创建(无论成功与否)完成时发生。
-
监听时间线事件 event 于 this.
[[device]], 后续步骤于 设备时间线执行。
设备时间线步骤:-
-
在 contentTimeline 上执行下列步骤:
-
返回。
注意: 设备丢失不会产生错误。参见 § 22 错误与调试。
-
-
若 pipeline 无效 且 error 为 内部错误,在 contentTimeline 上执行下列步骤并返回:
内容时间线步骤:-
reject promise,返回
GPUPipelineError,reason为"internal"。
-
-
若 pipeline 无效 且 error 为 校验错误,在 contentTimeline 上执行下列步骤并返回:
内容时间线步骤:-
reject promise,返回
GPUPipelineError,reason为"validation"。
-
-
参数:
-
GPURenderPipelineDescriptordescriptor -
GPUPipelineLayoutlayout -
GPUDevicedevice
设备时间线步骤:
-
若下列所有条件均满足则返回
true:-
验证 GPUVertexState(device, descriptor.
vertex, layout) 成功。 -
-
验证 GPUFragmentState(device, descriptor.
fragment, layout) 成功。 -
若 sample_mask 内建变量为 descriptor.
fragment的 着色器阶段输出:-
descriptor.
multisample.alphaToCoverageEnabled为false。
-
-
若 frag_depth 内建变量为 descriptor.
fragment的 着色器阶段输出:-
descriptor.
depthStencil已提供,且 descriptor.depthStencil.format必须带有 深度通道。
-
-
-
验证 GPUPrimitiveState(descriptor.
primitive, device) 成功。 -
如 descriptor.
depthStencil已提供:-
验证 GPUDepthStencilState(descriptor.
depthStencil, descriptor.primitive.topology) 成功。
-
-
验证 GPUMultisampleState(descriptor.
multisample) 成功。 -
如 descriptor.
multisample.alphaToCoverageEnabled为 true: -
必须至少存在一个附件:
-
存在 descriptor.
depthStencil。
-
验证阶段间接口(validating inter-stage interfaces)(device, descriptor) 返回
true。
-
参数:
-
GPUDevicedevice -
GPURenderPipelineDescriptordescriptor
返回值: boolean
设备时间线步骤:
-
令 maxVertexShaderOutputVariables = device.limits.
maxInterStageShaderVariables。 -
令 maxVertexShaderOutputLocation = device.limits.
maxInterStageShaderVariables- 1。 -
如 descriptor.
primitive.topology为"point-list":-
令 maxVertexShaderOutputVariables 减 1。
-
-
如 clip_distances 在 descriptor.
vertex的输出中声明:-
令 clipDistancesSize 为 clip_distances 的数组大小。
-
令 maxVertexShaderOutputVariables 减 ceil(clipDistancesSize / 4)。
-
令 maxVertexShaderOutputLocation 减 ceil(clipDistancesSize / 4)。
-
-
如下要求有任意一项不满足则返回
false:-
用户自定义顶点输出数量不超过 maxVertexShaderOutputVariables。
-
每个用户自定义顶点输出的location ≤ maxVertexShaderOutputLocation。
-
-
-
令 maxFragmentShaderInputVariables = device.limits.
maxInterStageShaderVariables。 -
如
front_facing、sample_index或sample_mask内建变量为 descriptor.fragment的输入:-
令 maxFragmentShaderInputVariables 减 1。
-
-
如有下列任一项不满足则返回
false: -
断言每个用户自定义片元输入的location均小于 device.limits.
maxInterStageShaderVariables。(由上规则推得)
-
-
返回
true。
GPURenderPipeline:
const renderPipeline= gpuDevice. createRenderPipeline({ layout: pipelineLayout, vertex: { module: shaderModule, entryPoint: 'vertexMain' }, fragment: { module: shaderModule, entryPoint: 'fragmentMain' , targets: [{ format: 'bgra8unorm' , }], } });
10.3.2. 图元状态(Primitive State)
dictionary {GPUPrimitiveState GPUPrimitiveTopology topology = "triangle-list";GPUIndexFormat stripIndexFormat ;GPUFrontFace frontFace = "ccw";GPUCullMode cullMode = "none"; // 需要 "depth-clip-control" 特性。boolean unclippedDepth =false ; };
GPUPrimitiveState
拥有以下成员,描述 GPURenderPipeline
如何根据顶点输入构造和光栅化图元:
topology,类型为 GPUPrimitiveTopology,默认值为"triangle-list"-
由顶点输入生成的图元类型。
stripIndexFormat, 类型为 GPUIndexFormat-
用于带 strip 拓扑 (
"line-strip"或"triangle-strip") 的管线, 决定索引缓冲区格式和图元重启值 ("uint16"/0xFFFF或"uint32"/0xFFFFFFFF)。 非 strip 拓扑管线不允许设置此项。注意: 某些实现需要提前知道图元重启值,以编译管线状态对象。
若 strip 拓扑管线用于带索引的绘制调用 (
drawIndexed()或drawIndexedIndirect()), 则必须设置该值,并且要与 draw 调用所用索引缓冲区格式(setIndexBuffer()设置)一致。详见 § 23.2.3 图元组装。
frontFace,类型为 GPUFrontFace,默认值为"ccw"-
定义哪些多边形被视为正面(front-facing)。
cullMode,类型为 GPUCullMode,默认值为"none"-
定义哪些多边形朝向会被裁剪(cull),如有的话。
unclippedDepth, 类型为 boolean,默认值为false-
如为 true,表示深度裁剪(depth clipping)被禁用。
需要启用
"depth-clip-control"特性。
-
GPUPrimitiveStatedescriptor -
GPUDevicedevice
设备时间线步骤:
-
如果下列所有条件都满足则返回
true:-
如 descriptor.
topology非"line-strip"或"triangle-strip":-
descriptor.
stripIndexFormat不得提供。
-
-
如 descriptor.
unclippedDepth为true:-
"depth-clip-control"必须启用于 device。
-
-
enum {GPUPrimitiveTopology "point-list" ,"line-list" ,"line-strip" ,"triangle-list" ,"triangle-strip" , };
GPUPrimitiveTopology
定义了 GPURenderPipeline
的绘制调用会使用的图元类型。详见 § 23.2.5 光栅化:
"point-list"-
每个顶点定义一个点图元。
"line-list"-
每两个连续顶点组成一个线段图元。
"line-strip"-
第一个顶点后,每个顶点与前一个顶点组成一个线段图元。
"triangle-list"-
每三个连续顶点组成一个三角形图元。
"triangle-strip"-
前两个顶点后,每个顶点与前两个顶点组成一个三角形图元。
enum {GPUFrontFace "ccw" ,"cw" , };
GPUFrontFace
定义 GPURenderPipeline
视为正面(front-facing)的多边形。详见 § 23.2.5.4 多边形光栅化:
enum {GPUCullMode "none" ,"front" ,"back" , };
GPUPrimitiveTopology
定义了哪些多边形会被 GPURenderPipeline
的绘制调用裁剪。详见 § 23.2.5.4 多边形光栅化:
注意: GPUFrontFace
和 GPUCullMode
对于 "point-list"、
"line-list"、
"line-strip"
拓扑无效。
10.3.3. 多重采样状态(Multisample State)
dictionary {GPUMultisampleState GPUSize32 count = 1;GPUSampleMask mask = 0xFFFFFFFF;boolean alphaToCoverageEnabled =false ; };
GPUMultisampleState
拥有以下成员,描述 GPURenderPipeline
如何与渲染通道的多重采样附件进行交互。
count,类型为 GPUSize32,默认值为1-
每像素的采样数。该
GPURenderPipeline仅与colorAttachments和depthStencilAttachment中sampleCount匹配的附件纹理兼容。 mask,类型为 GPUSampleMask,默认值0xFFFFFFFF-
决定哪些采样点会被写入的掩码。
alphaToCoverageEnabled, 类型为 boolean,默认值false-
为
true时,表示片元的 alpha 通道用于生成采样覆盖掩码。
-
GPUMultisampleStatedescriptor
设备时间线步骤:
-
若下列所有条件均满足则返回
true:-
descriptor.
count必须为 1 或 4。 -
如 descriptor.
alphaToCoverageEnabled为true:-
descriptor.
count> 1。
-
-
10.3.4. 片元状态(Fragment State)
dictionary :GPUFragmentState GPUProgrammableStage {required sequence <GPUColorTargetState ?>targets ; };
targets,类型为sequence<GPUColorTargetState?>-
一个
GPUColorTargetState列表,定义本管线写入的颜色目标的格式和行为。
参数:
-
GPUDevicedevice -
GPUFragmentStatedescriptor -
GPUPipelineLayoutlayout
设备时间线步骤:
-
若下列所有要求都满足则返回
true:-
验证 GPUProgrammableStage(
FRAGMENT, descriptor, layout, device) 成功。 -
descriptor.
targets.size 必须 ≤ device.[[limits]].maxColorAttachments。 -
令 usesDualSourceBlending =
false。 -
遍历 descriptor.
targets的每个包含非null值 colorState 的 index:-
colorState.
format必须在 § 26.1.1 普通颜色格式中列出,并具有RENDER_ATTACHMENT能力。 -
colorState.
writeMask必须小于 16。 -
-
colorState.
blend.color必须为有效 GPUBlendComponent。 -
colorState.
blend.alpha必须为有效 GPUBlendComponent。 -
如 colorState.
blend.color.srcFactor或 colorState.blend.color.dstFactor或 colorState.blend.alpha.srcFactor或 colorState.blend.alpha.dstFactor使用了对应混合单元的第二输入(即任一为"src1"、"one-minus-src1"、"src1-alpha"、"one-minus-src1-alpha"):-
将 usesDualSourceBlending 设为
true。
-
-
对于 着色器阶段输出值 output,其 location 属性等于 index:
-
对于 colorState.
format的每个分量,output 中必须有对应分量。 (即 RGBA 需 vec4,RGB 需 vec3 或 vec4,RG 需 vec2 或 vec3 或 vec4。) -
如 colorState.
format的GPUTextureSampleType(见 § 26.1 纹理格式能力)为:"float"和/或"unfilterable-float"-
output 必须为浮点标量类型。
"sint"-
output 必须为有符号整数标量类型。
"uint"-
output 必须为无符号整数标量类型。
-
如 colorState.
blend已提供,且 colorState.blend.color.srcFactor或 .dstFactor使用了源 alpha (即任一为"src-alpha"、"one-minus-src-alpha"、"src-alpha-saturated"、"src1-alpha"、"one-minus-src1-alpha"):-
output 必须有 alpha 通道(即为 vec4)。
-
-
-
如 colorState.
writeMask非 0:
-
-
如 usesDualSourceBlending 为
true: -
验证 GPUFragmentState 的 color attachment bytes per sample(device, descriptor.
targets) 成功。
-
参数:
-
GPUDevicedevice -
sequence<
GPUColorTargetState?> targets
设备时间线步骤:
-
令 formats 为一个空的 list<
GPUTextureFormat?> -
遍历 targets 中的每个 target:
-
计算 color attachment bytes per sample(formats) 必须 ≤ device.
[[limits]].maxColorAttachmentBytesPerSample。
注意: 片元着色器可输出的值可能多于管线实际使用的颜色输出,未被使用的值会被忽略。
GPUBlendComponent
component 是一个 有效 GPUBlendComponent(valid GPUBlendComponent),在逻辑
device device 上,需满足以下要求:
10.3.5. 颜色目标状态(Color Target State)
dictionary {GPUColorTargetState required GPUTextureFormat format ;GPUBlendState blend ;GPUColorWriteFlags writeMask = 0xF; // GPUColorWrite.ALL };
format,类型为 GPUTextureFormat-
该颜色目标的
GPUTextureFormat。管线只与 在对应颜色附件中使用该格式的GPURenderPassEncoder的GPUTextureView兼容。 blend,类型为 GPUBlendState-
该颜色目标的混合行为。如未定义,则禁用该颜色目标的混合。
writeMask,类型为 GPUColorWriteFlags,默认值0xF-
控制绘制到该颜色目标时写入哪些通道的位掩码。
dictionary {GPUBlendState required GPUBlendComponent color ;required GPUBlendComponent alpha ; };
color,类型为 GPUBlendComponent-
定义对应渲染目标颜色通道的混合行为。
alpha,类型为 GPUBlendComponent-
定义对应渲染目标 alpha 通道的混合行为。
typedef [EnforceRange ]unsigned long ; [GPUColorWriteFlags Exposed =(Window ,Worker ),SecureContext ]namespace {GPUColorWrite const GPUFlagsConstant = 0x1;RED const GPUFlagsConstant = 0x2;GREEN const GPUFlagsConstant = 0x4;BLUE const GPUFlagsConstant = 0x8;ALPHA const GPUFlagsConstant = 0xF; };ALL
10.3.5.1. 混合状态(Blend State)
dictionary {GPUBlendComponent GPUBlendOperation operation = "add";GPUBlendFactor srcFactor = "one";GPUBlendFactor dstFactor = "zero"; };
GPUBlendComponent
拥有以下成员,描述片元的颜色或 alpha 分量是如何混合的:
operation,类型为 GPUBlendOperation,默认值为"add"-
定义用于计算写入目标附件分量值的
GPUBlendOperation。 srcFactor,类型为 GPUBlendFactor,默认值为"one"-
定义对来自片元着色器的值执行的
GPUBlendFactor操作。 dstFactor,类型为 GPUBlendFactor,默认值为"zero"-
定义对来自目标附件的值执行的
GPUBlendFactor操作。
下表说明了用于描述片元位置的颜色分量的符号:
RGBAsrc
| 片元着色器输出到颜色附件的颜色。 如果着色器没有返回 alpha 通道,则不能使用 src-alpha 混合因子。 |
RGBAsrc1
| 片元着色器输出到带有
"@blend_src" 属性为 1 的颜色附件的颜色。
如果着色器没有返回 alpha 通道,则不能使用 src1-alpha 混合因子。
|
RGBAdst
| 当前颜色附件中的颜色。
缺失的绿色/蓝色/alpha 通道分别默认 0, 0, 1。
|
RGBAconst
| 当前 [[blendConstant]]。
|
RGBAsrcFactor
| 由 srcFactor
定义的源混合因子分量。
|
RGBAdstFactor
| 由 dstFactor
定义的目标混合因子分量。
|
enum {GPUBlendFactor "zero" ,"one" ,"src" ,"one-minus-src" ,"src-alpha" ,"one-minus-src-alpha" ,"dst" ,"one-minus-dst" ,"dst-alpha" ,"one-minus-dst-alpha" ,"src-alpha-saturated" ,"constant" ,"one-minus-constant" ,"src1" ,"one-minus-src1" ,"src1-alpha" ,"one-minus-src1-alpha" , };
GPUBlendFactor
定义了源或目标混合因子的计算方式:
| GPUBlendFactor | 混合因子 RGBA 分量 | 特性 |
|---|---|---|
"zero"
| (0, 0, 0, 0)
| |
"one"
| (1, 1, 1, 1)
| |
"src"
| (Rsrc, Gsrc, Bsrc, Asrc)
| |
"one-minus-src"
| (1 - Rsrc, 1 - Gsrc, 1 - Bsrc, 1 - Asrc)
| |
"src-alpha"
| (Asrc, Asrc, Asrc, Asrc)
| |
"one-minus-src-alpha"
| (1 - Asrc, 1 - Asrc, 1 - Asrc, 1 - Asrc)
| |
"dst"
| (Rdst, Gdst, Bdst, Adst)
| |
"one-minus-dst"
| (1 - Rdst, 1 - Gdst, 1 - Bdst, 1 - Adst)
| |
"dst-alpha"
| (Adst, Adst, Adst, Adst)
| |
"one-minus-dst-alpha"
| (1 - Adst, 1 - Adst, 1 - Adst, 1 - Adst)
| |
"src-alpha-saturated"
| (min(Asrc, 1 - Adst), min(Asrc, 1 - Adst), min(Asrc, 1 - Adst), 1)
| |
"constant"
| (Rconst, Gconst, Bconst, Aconst)
| |
"one-minus-constant"
| (1 - Rconst, 1 - Gconst, 1 - Bconst, 1 - Aconst)
| |
"src1"
| (Rsrc1, Gsrc1, Bsrc1, Asrc1)
| dual-source-blending
|
"one-minus-src1"
| (1 - Rsrc1, 1 - Gsrc1, 1 - Bsrc1, 1 - Asrc1)
| |
"src1-alpha"
| (Asrc1, Asrc1, Asrc1, Asrc1)
| |
"one-minus-src1-alpha"
| (1 - Asrc1, 1 - Asrc1, 1 - Asrc1, 1 - Asrc1)
|
enum {GPUBlendOperation "add" ,"subtract" ,"reverse-subtract" ,"min" ,"max" , };
GPUBlendOperation
定义了用于组合源和目标混合因子的算法:
| GPUBlendOperation | RGBA 分量 |
|---|---|
"add"
| RGBAsrc × RGBAsrcFactor + RGBAdst × RGBAdstFactor
|
"subtract"
| RGBAsrc × RGBAsrcFactor - RGBAdst × RGBAdstFactor
|
"reverse-subtract"
| RGBAdst × RGBAdstFactor - RGBAsrc × RGBAsrcFactor
|
"min"
| min(RGBAsrc, RGBAdst)
|
"max"
| max(RGBAsrc, RGBAdst)
|
10.3.6. 深度/模板状态(Depth/Stencil State)
dictionary {GPUDepthStencilState required GPUTextureFormat format ;boolean depthWriteEnabled ;GPUCompareFunction depthCompare ;GPUStencilFaceState stencilFront = {};GPUStencilFaceState stencilBack = {};GPUStencilValue stencilReadMask = 0xFFFFFFFF;GPUStencilValue stencilWriteMask = 0xFFFFFFFF;GPUDepthBias depthBias = 0;float depthBiasSlopeScale = 0;float depthBiasClamp = 0; };
GPUDepthStencilState
拥有以下成员,描述 GPURenderPipeline
如何影响渲染通道的 depthStencilAttachment:
format,类型为 GPUTextureFormat-
该
GPURenderPipeline可兼容的depthStencilAttachment的format。 depthWriteEnabled,类型为 boolean-
指示该
GPURenderPipeline是否可以修改depthStencilAttachment的深度值。 depthCompare,类型为 GPUCompareFunction-
用于将片元深度与
depthStencilAttachment深度值进行比较的操作。 stencilFront,类型为 GPUStencilFaceState,默认值为{}-
定义前面图元的模板比较和操作方式。
stencilBack,类型为 GPUStencilFaceState,默认值为{}-
定义背面图元的模板比较和操作方式。
stencilReadMask,类型为 GPUStencilValue,默认值0xFFFFFFFF-
控制执行模板测试时读取
depthStencilAttachment模板值哪些位的位掩码。 stencilWriteMask,类型为 GPUStencilValue,默认值0xFFFFFFFF-
控制执行模板操作时写入
depthStencilAttachment模板值哪些位的位掩码。 depthBias,类型为 GPUDepthBias,默认值0-
加到每个三角形片元的恒定深度偏移。详见 带偏移的片元深度(biased fragment depth)。
depthBiasSlopeScale,类型为 float,默认值0-
随三角形片元斜率变化的深度偏移。详见 带偏移的片元深度(biased fragment depth)。
depthBiasClamp,类型为 float,默认值0-
三角形片元的最大深度偏移。详见 带偏移的片元深度(biased fragment depth)。
注意: depthBias、
depthBiasSlopeScale、
depthBiasClamp
对 "point-list"、
"line-list"、
"line-strip"
图元无效,且必须为 0。
GPUDepthStencilState
state 绘制时写入 depthStencilAttachment
attachment 的片元,计算方式如下(队列时间线步骤):
-
令 r 为 format 转为 32 位 float 后大于
0的最小正可表示值。 -
令 maxDepthSlope 为片元深度值的水平和垂直斜率中的最大值。
-
如 format 为 unorm 格式:
-
令 bias 为
(float)state.。depthBias* r + state.depthBiasSlopeScale* maxDepthSlope
-
-
否则,如 format 为 float 格式:
-
令 bias 为
(float)state.。depthBias* 2^(exp(primitive 最大深度) - r) + state.depthBiasSlopeScale* maxDepthSlope
-
-
如 state.
depthBiasClamp>0:-
将 bias 设为
min(state.。depthBiasClamp, bias)
-
-
否则如 state.
depthBiasClamp<0:-
将 bias 设为
max(state.。depthBiasClamp, bias)
-
-
如 state.
depthBias≠0或 state.depthBiasSlopeScale≠0:-
将片元深度值设为
片元深度值 + bias
-
参数:
-
GPUDepthStencilStatedescriptor -
GPUPrimitiveTopologytopology
设备时间线步骤:
-
当且仅当下列所有条件都满足时返回
true:-
如 descriptor.
depthWriteEnabled为true,或 descriptor.depthCompare已提供且不为"always":-
descriptor.
format必须包含深度分量。
-
-
如 descriptor.
stencilFront或 descriptor.stencilBack非默认值:-
descriptor.
format必须包含模板分量。
-
-
如 descriptor.
format含有深度分量:-
descriptor.
depthWriteEnabled必须已提供。 -
如满足以下任一条件,descriptor.
depthCompare必须已提供:-
descriptor.
depthWriteEnabled为true,或 -
descriptor.
stencilFront.depthFailOp非"keep",或 -
descriptor.
stencilBack.depthFailOp非"keep"。
-
-
-
如 topology 为
"point-list"、"line-list"、"line-strip":-
descriptor.
depthBias必须为 0。 -
descriptor.
depthBiasSlopeScale必须为 0。 -
descriptor.
depthBiasClamp必须为 0。
-
dictionary {GPUStencilFaceState GPUCompareFunction compare = "always";GPUStencilOperation failOp = "keep";GPUStencilOperation depthFailOp = "keep";GPUStencilOperation passOp = "keep"; };
GPUStencilFaceState
拥有以下成员,描述模板比较和操作的方式:
compare,类型为 GPUCompareFunction,默认值为"always"-
在用
[[stencilReference]]的值与片元depthStencilAttachment的模板值进行测试时使用的GPUCompareFunction。 failOp,类型为 GPUStencilOperation,默认值为"keep"-
如片元模板比较测试(由
compare描述)失败,则执行的GPUStencilOperation操作。 depthFailOp,类型为 GPUStencilOperation,默认值为"keep"-
如片元深度比较(由
depthCompare描述)失败,则执行的GPUStencilOperation操作。 passOp,类型为 GPUStencilOperation,默认值为"keep"-
如片元模板比较测试(由
compare描述)通过,则执行的GPUStencilOperation操作。
enum {GPUStencilOperation "keep" ,"zero" ,"replace" ,"invert" ,"increment-clamp" ,"decrement-clamp" ,"increment-wrap" ,"decrement-wrap" , };
GPUStencilOperation
定义了以下操作:
"keep"-
保持当前模板值。
"zero"-
将模板值设为
0。 "replace"-
将模板值设为
[[stencilReference]]。 "invert"-
对当前模板值取按位非。
"increment-clamp"-
将当前模板值加一,并钳制到
depthStencilAttachment模板分量的最大可表示值。 "decrement-clamp"-
将当前模板值减一,并钳制到
0。 "increment-wrap"-
将当前模板值加一,如超过
depthStencilAttachment模板分量最大可表示值,则回绕为 0。 "decrement-wrap"-
将当前模板值减一,如小于
0,则回绕为depthStencilAttachment模板分量最大可表示值。
10.3.7. 顶点状态(Vertex State)
enum {GPUIndexFormat "uint16" ,"uint32" , };
索引格式(index format)决定了缓冲区中索引值的数据类型,并且在用于 strip 图元拓扑("line-strip"
或
"triangle-strip")时,也指定了图元重启值(primitive
restart value)。图元重启值(primitive restart value)
表示哪个索引值意味着应当新建一个图元,而不是继续用先前的索引顶点构建三角带。
GPUPrimitiveState
指定 strip 图元拓扑时,若用于带索引绘制,必须指定
stripIndexFormat,
以便在管线创建时就明确将使用的图元重启值。
指定 list 图元拓扑的
GPUPrimitiveState
在索引渲染时会使用 setIndexBuffer()
传递的索引格式。
| 索引格式(Index format) | 字节数(Byte size) | 图元重启值(Primitive restart value) |
|---|---|---|
"uint16"
| 2 | 0xFFFF |
"uint32"
| 4 | 0xFFFFFFFF |
10.3.7.1. 顶点格式(Vertex Formats)
GPUVertexFormat
指定顶点属性的数据格式,决定如何从顶点缓冲区读取数据并传递到着色器。格式名表明了分量的顺序、每个分量的比特数,以及该分量的顶点数据类型。
每种顶点数据类型(vertex data type) 都可以映射到任意同基类型的WGSL 标量类型,无论每个分量有多少位:
| 顶点格式前缀(Vertex format prefix) | 顶点数据类型(Vertex data type) | 兼容 WGSL 类型(Compatible WGSL types) |
|---|---|---|
uint
| unsigned int(无符号整数) | u32
|
sint
| signed int(有符号整数) | i32
|
unorm
| unsigned normalized(无符号归一化) | f16, f32
|
snorm
| signed normalized(有符号归一化) | |
float
| floating point(浮点数) |
多分量格式的“x”后面的数字表示分量数量。顶点格式和着色器类型的分量数不一致是允许的,多余分量会被丢弃,不足分量则用默认值补齐。
"unorm8x2"
,字节值为 [0x7F, 0xFF]
,可在着色器中通过如下类型访问:
| 着色器类型(Shader type) | 着色器值(Shader value) |
|---|---|
f16
| 0.5h
|
f32
| 0.5f
|
vec2<f16>
| vec2(0.5h, 1.0h)
|
vec2<f32>
| vec2(0.5f, 1.0f)
|
vec3<f16>
| vec2(0.5h, 1.0h, 0.0h)
|
vec3<f32>
| vec2(0.5f, 1.0f, 0.0f)
|
vec4<f16>
| vec2(0.5h, 1.0h, 0.0h, 1.0h)
|
vec4<f32>
| vec2(0.5f, 1.0f, 0.0f, 1.0f)
|
关于顶点格式如何在着色器中暴露,详见 § 23.2.2 顶点处理。
enum {GPUVertexFormat "uint8" ,"uint8x2" ,"uint8x4" ,"sint8" ,"sint8x2" ,"sint8x4" ,"unorm8" ,"unorm8x2" ,"unorm8x4" ,"snorm8" ,"snorm8x2" ,"snorm8x4" ,"uint16" ,"uint16x2" ,"uint16x4" ,"sint16" ,"sint16x2" ,"sint16x4" ,"unorm16" ,"unorm16x2" ,"unorm16x4" ,"snorm16" ,"snorm16x2" ,"snorm16x4" ,"float16" ,"float16x2" ,"float16x4" ,"float32" ,"float32x2" ,"float32x3" ,"float32x4" ,"uint32" ,"uint32x2" ,"uint32x3" ,"uint32x4" ,"sint32" ,"sint32x2" ,"sint32x3" ,"sint32x4" ,"unorm10-10-10-2" ,"unorm8x4-bgra" , };
| 顶点格式(Vertex format) | 数据类型(Data type) | 分量数(Components) | 字节数(byteSize) | 示例 WGSL 类型(Example WGSL type) |
|---|---|---|---|---|
"uint8"
| 无符号整数(unsigned int) | 1 | 1 | u32
|
"uint8x2"
| 无符号整数 | 2 | 2 | vec2<u32>
|
"uint8x4"
| 无符号整数 | 4 | 4 | vec4<u32>
|
"sint8"
| 有符号整数(signed int) | 1 | 1 | i32
|
"sint8x2"
| 有符号整数 | 2 | 2 | vec2<i32>
|
"sint8x4"
| 有符号整数 | 4 | 4 | vec4<i32>
|
"unorm8"
| 无符号归一化(unsigned normalized) | 1 | 1 | f32
|
"unorm8x2"
| 无符号归一化 | 2 | 2 | vec2<f32>
|
"unorm8x4"
| 无符号归一化 | 4 | 4 | vec4<f32>
|
"snorm8"
| 有符号归一化(signed normalized) | 1 | 1 | f32
|
"snorm8x2"
| 有符号归一化 | 2 | 2 | vec2<f32>
|
"snorm8x4"
| 有符号归一化 | 4 | 4 | vec4<f32>
|
"uint16"
| 无符号整数 | 1 | 2 | u32
|
"uint16x2"
| 无符号整数 | 2 | 4 | vec2<u32>
|
"uint16x4"
| 无符号整数 | 4 | 8 | vec4<u32>
|
"sint16"
| 有符号整数 | 1 | 2 | i32
|
"sint16x2"
| 有符号整数 | 2 | 4 | vec2<i32>
|
"sint16x4"
| 有符号整数 | 4 | 8 | vec4<i32>
|
"unorm16"
| 无符号归一化 | 1 | 2 | f32
|
"unorm16x2"
| 无符号归一化 | 2 | 4 | vec2<f32>
|
"unorm16x4"
| 无符号归一化 | 4 | 8 | vec4<f32>
|
"snorm16"
| 有符号归一化 | 1 | 2 | f32
|
"snorm16x2"
| 有符号归一化 | 2 | 4 | vec2<f32>
|
"snorm16x4"
| 有符号归一化 | 4 | 8 | vec4<f32>
|
"float16"
| 浮点数(float) | 1 | 2 | f32
|
"float16x2"
| 浮点数 | 2 | 4 | vec2<f16>
|
"float16x4"
| 浮点数 | 4 | 8 | vec4<f16>
|
"float32"
| 浮点数 | 1 | 4 | f32
|
"float32x2"
| 浮点数 | 2 | 8 | vec2<f32>
|
"float32x3"
| 浮点数 | 3 | 12 | vec3<f32>
|
"float32x4"
| 浮点数 | 4 | 16 | vec4<f32>
|
"uint32"
| 无符号整数 | 1 | 4 | u32
|
"uint32x2"
| 无符号整数 | 2 | 8 | vec2<u32>
|
"uint32x3"
| 无符号整数 | 3 | 12 | vec3<u32>
|
"uint32x4"
| 无符号整数 | 4 | 16 | vec4<u32>
|
"sint32"
| 有符号整数 | 1 | 4 | i32
|
"sint32x2"
| 有符号整数 | 2 | 8 | vec2<i32>
|
"sint32x3"
| 有符号整数 | 3 | 12 | vec3<i32>
|
"sint32x4"
| 有符号整数 | 4 | 16 | vec4<i32>
|
"unorm10-10-10-2"
| 无符号归一化 | 4 | 4 | vec4<f32>
|
"unorm8x4-bgra"
| 无符号归一化 | 4 | 4 | vec4<f32>
|
enum {GPUVertexStepMode "vertex" ,"instance" , };
步进模式(step mode)配置了如何根据当前的顶点索引或实例索引计算顶点缓冲区数据的地址:
"vertex"-
每个顶点地址按
arrayStride递增, 在不同实例之间重置。 "instance"-
每个实例地址按
arrayStride递增。
dictionary :GPUVertexState GPUProgrammableStage {sequence <GPUVertexBufferLayout ?>buffers = []; };
buffers,类型为sequence<GPUVertexBufferLayout?>,默认值为[]-
一个
GPUVertexBufferLayout列表,定义了本管线所用顶点缓冲区中顶点属性数据的布局。
顶点缓冲区(vertex
buffer)在概念上是对缓冲区内存的视图,把它看作“结构体数组”。arrayStride
是该数组中每个元素(结构)之间的字节跨度。每个顶点缓冲区的元素就像一个结构体,其内存布局由 attributes
定义,attributes 描述了结构体成员。
每个 GPUVertexAttribute
描述了该成员的 format
以及在结构体中的 offset
字节偏移量。
每个 attribute 在顶点着色器中表现为一个独立输入,绑定一个数值类型的 location,由 shaderLocation
指定。每个 location 在 GPUVertexState
内必须唯一。
dictionary {GPUVertexBufferLayout required GPUSize64 arrayStride ;GPUVertexStepMode stepMode = "vertex";required sequence <GPUVertexAttribute >attributes ; };
arrayStride,类型为 GPUSize64-
该数组元素之间的字节跨度。
stepMode,类型为 GPUVertexStepMode,默认值为"vertex"-
该数组每个元素是按顶点还是按实例计(per-vertex 或 per-instance)。
attributes,类型为 sequence<GPUVertexAttribute>-
定义每个元素内各顶点属性(结构体成员)的数组。
dictionary {GPUVertexAttribute required GPUVertexFormat format ;required GPUSize64 offset ;required GPUIndex32 shaderLocation ; };
format,类型为 GPUVertexFormat-
该属性的
GPUVertexFormat。 offset,类型为 GPUSize64-
从该结构体起始到该属性数据的字节偏移量。
shaderLocation,类型为 GPUIndex32-
该属性关联的数值 location,需与 "@location" 属性 在
vertex.module中对应。
参数:
-
GPUDevicedevice -
GPUVertexBufferLayoutdescriptor
设备时间线步骤:
-
当且仅当下列所有条件都满足时,返回
true:-
descriptor.
arrayStride≤ device.[[device]].[[limits]].maxVertexBufferArrayStride。 -
descriptor.
arrayStride是 4 的倍数。 -
对于 descriptor.
attributes列表中的每个属性 attrib:-
如果 descriptor.
arrayStride为 0:-
attrib.
offset+ byteSize(attrib.format) ≤ device.[[device]].[[limits]].maxVertexBufferArrayStride。
否则:
-
attrib.
offset+ byteSize(attrib.format) ≤ descriptor.arrayStride。
-
-
attrib.
shaderLocation必须小于 device.[[device]].[[limits]].maxVertexAttributes。
-
-
参数:
-
GPUDevicedevice -
GPUVertexStatedescriptor -
GPUPipelineLayoutlayout
设备时间线步骤:
-
断言(Assert) entryPoint 不为
null。 -
以下所有要求 必须满足:
-
验证 GPUProgrammableStage(
VERTEX, descriptor, layout, device) 必须成功。 -
descriptor.
buffers.size 必须 ≤ device.[[device]].[[limits]].maxVertexBuffers。 -
列表 descriptor.
buffers中的每个 vertexBuffer 描述符 必须通过 验证 GPUVertexBufferLayout(device, vertexBuffer)。 -
所有 vertexBuffer.
attributes.size 之和, 必须 ≤ device.[[device]].[[limits]].maxVertexAttributes。 -
对于 entryPoint 静态使用的每个顶点属性声明(位置 location,类型 T), 必须存在且仅存在一组 (i, j) 使得 descriptor.
buffers[i]? .attributes[j] .shaderLocation== location。令 attrib 为该
GPUVertexAttribute。 -
T 必须与 attrib.
format的 顶点数据类型(vertex data type)兼容:- "unorm"、"snorm" 或 "float"
-
T 必须是
f32或vecN<f32>。 - "uint"
-
T 必须是
u32或vecN<u32>。 - "sint"
-
T 必须是
i32或vecN<i32>。
-
11. 复制(Copies)
11.1. 缓冲区复制(Buffer Copies)
缓冲区复制操作基于原始字节执行。
WebGPU 提供了“缓冲式” GPUCommandEncoder
命令:
以及“即时” GPUQueue
操作:
-
writeBuffer(), 用于ArrayBuffer到GPUBuffer的写入
11.2. 纹素复制(Texel Copies)
纹素复制(Texel copy)操作针对的是纹理/图像数据,而不是字节。
WebGPU 提供“缓冲式” GPUCommandEncoder
命令:
以及“即时” GPUQueue
操作:
-
writeTexture(), 用于ArrayBuffer到GPUTexture的写入 -
copyExternalImageToTexture(), 用于从 Web 平台图片源复制到纹理
在纹素复制过程中,纹素会以等价纹素表示(equivalent texel representation)被复制。 纹素复制仅保证源中的有效、正常数字值在目标中具有相同的数值,但可能不会保留以下值的位表示:
-
snorm 类型值的 -1.0 可能被表示为 -127 或 -128。
-
零值的符号可能不会被保留。
-
非正规浮点值可能会被替换为 -0.0 或 +0.0。
-
任何 NaN 或 Infinity 都是无效值,可能会被替换为不确定值(indeterminate value)。
注意: 复制操作可能通过 WGSL 着色器实现,所以可能会观察到任何文档中描述的 WGSL 浮点行为。
下列定义被这些方法所用:
11.2.1. GPUTexelCopyBufferLayout
"GPUTexelCopyBufferLayout"
描述了在“缓冲区”字节(GPUBuffer
或 AllowSharedBufferSource)
中的纹素在“纹素复制”操作中的“布局”。
dictionary GPUTexelCopyBufferLayout {GPUSize64 offset = 0;GPUSize32 bytesPerRow ;GPUSize32 rowsPerImage ; };
纹素图像(texel image)由一行或多行纹素块组成,这里称为纹素块行(texel block
row)。每个texel image的texel block row数量相同,且所有纹素块的GPUTextureFormat一致。
GPUTexelCopyBufferLayout
描述了线性内存中多个texel image的布局。当在texture与GPUBuffer之间复制数据,或通过GPUQueue写入texture时会用到。
在字节数组与纹理之间的复制操作总是以整个 纹素块为单位。无法只更新一个纹素块的一部分。
纹素块在每个texel block row中在内存中紧密排列,每个相邻的纹素块之间无填充,包括对深度或模板纹理特定
aspect 的复制:模板值以字节数组紧密排列,深度值以相应类型("depth16unorm" 或
"depth32float")数组紧密排列。
offset,类型为 GPUSize64,默认值为0-
从纹素数据源(如
GPUTexelCopyBufferInfo.buffer) 起始到纹素数据起始位置的字节偏移量。 bytesPerRow,类型为 GPUSize32-
若存在多个 纹素块行(即复制高度或深度大于一个块)时为必填项。
rowsPerImage,类型为 GPUSize32-
每个 纹素图像 包含的 纹素块行数量。
rowsPerImage×bytesPerRow即为每个 纹素图像 起始到下一个 纹素图像 起始的字节跨度。若存在多个 纹素图像(即复制深度大于 1)时为必填项。
11.2.2. GPUTexelCopyBufferInfo
"GPUTexelCopyBufferInfo"
描述了作为“缓冲区”源或目标的 GPUBuffer 和
GPUTexelCopyBufferLayout
的“参数信息”,用于“纹素复制”操作。
它与 copySize 一起描述了 GPUBuffer
中某一区域纹素的内存布局。
dictionary GPUTexelCopyBufferInfo :GPUTexelCopyBufferLayout {required GPUBuffer buffer ; };
buffer,类型为 GPUBuffer-
根据所调用的方法,该缓冲区要么包含要被复制的纹素数据,要么用于存储被复制的纹素数据。
参数:
-
GPUTexelCopyBufferInfoimageCopyBuffer
返回: boolean
设备时间线步骤:
-
当且仅当下列所有条件都满足时返回
true:-
imageCopyBuffer.
bytesPerRow必须是 256 的倍数。
11.2.3. GPUTexelCopyTextureInfo
"GPUTexelCopyTextureInfo"
描述了作为“纹理”源或目标的 GPUTexture
等对象在“纹素复制”操作中的“参数信息”。
它和 copySize 一起描述了纹理的一个子区域(跨越同一 mip-map 层级的一个或多个连续纹理子资源)。
dictionary GPUTexelCopyTextureInfo {required GPUTexture texture ;GPUIntegerCoordinate mipLevel = 0;GPUOrigin3D origin = {};GPUTextureAspect aspect = "all"; };
texture,类型为 GPUTexture-
要复制到或从其复制的纹理。
mipLevel,类型为 GPUIntegerCoordinate,默认值为0-
要复制到或从其复制的
texture的 mip-map 层级。 origin,类型为 GPUOrigin3D,默认值为{}-
定义复制的起点——要复制到或从其复制的纹理子区域的最小角落。与
copySize一起,定义完整的复制子区域。 aspect,类型为 GPUTextureAspect,默认值为"all"-
定义要复制到/从其复制的
texture的哪些 aspect。
GPUTexelCopyTextureInfo
copyTexture 的深度切片或数组层 index,按如下步骤确定:
GPUTexelCopyBufferLayout
bufferLayout 描述的数据,纹素块
x、y 在 GPUTexture
texture 的深度切片或数组层 z,按如下步骤确定:
-
令 imageOffset = (z × bufferLayout.
rowsPerImage× bufferLayout.bytesPerRow) + bufferLayout.offset。 -
令 rowOffset = (y × bufferLayout.
bytesPerRow) + imageOffset。 -
令 blockOffset = (x × blockBytes) + rowOffset。
-
返回 blockOffset。
参数:
-
GPUTexelCopyTextureInfotexelCopyTextureInfo -
GPUExtent3DcopySize
返回: boolean
设备时间线步骤:
-
当且仅当下列所有条件满足时返回
true:-
验证纹理复制范围(texelCopyTextureInfo, copySize) 返回
true。 -
texelCopyTextureInfo.
texture必须是一个 有效的GPUTexture。 -
texelCopyTextureInfo.
mipLevel必须小于 texelCopyTextureInfo.texture.mipLevelCount。 -
若下述任一条件为真,则 GPUTexelCopyTextureInfo 物理子资源尺寸等于 copySize:
-
texelCopyTextureInfo.
texture.sampleCount> 1。
-
参数:
-
GPUTexelCopyTextureInfotexelCopyTextureInfo -
GPUTexelCopyBufferLayoutbufferLayout -
GPUSize64OutdataLength -
GPUExtent3DcopySize -
GPUTextureUsagetextureUsage -
booleanaligned
返回: boolean
设备时间线步骤:
-
令 texture = texelCopyTextureInfo.
texture -
令 aspectSpecificFormat = texture.
format。 -
当且仅当下列所有条件满足时返回
true:-
验证 GPUTexelCopyTextureInfo(texelCopyTextureInfo, copySize) 返回
true。 -
texture.
sampleCount为 1。 -
texture.
usage包含 textureUsage。 -
-
如果 textureUsage 是:
COPY_SRC-
该 aspect 必须根据 § 26.1.2 深度模板格式 是有效的纹素复制源。
COPY_DST-
该 aspect 必须根据 § 26.1.2 深度模板格式 是有效的纹素复制目标。
-
根据 § 26.1.2 深度模板格式,将 aspectSpecificFormat 设为aspect-specific format。
-
将 offsetAlignment 设为 4。
-
如果 aligned 为
true:-
bufferLayout.
offset必须是 offsetAlignment 的倍数。
-
-
验证线性纹理数据(bufferLayout, dataLength, aspectSpecificFormat, copySize) 成功。
-
11.2.4.
GPUCopyExternalImageDestInfo
WebGPU 纹理保存的是原始数值数据,并没有附带描述颜色的语义元数据。
然而,copyExternalImageToTexture()
复制自带有颜色描述信息的源。
"GPUCopyExternalImageDestInfo"
描述了 "copyExternalImageToTexture()" 操作中 "目标(dest)" 的
"参数信息(info)"。
它是一个 GPUTexelCopyTextureInfo,但额外带有色彩空间/编码和
alpha 预乘元数据,以便在复制过程中保留语义色彩数据。
这些元数据仅影响复制操作的语义,不影响目标纹理对象本身的状态或语义。
dictionary GPUCopyExternalImageDestInfo :GPUTexelCopyTextureInfo {PredefinedColorSpace colorSpace = "srgb";boolean premultipliedAlpha =false ; };
colorSpace,类型为 PredefinedColorSpace,默认值为"srgb"-
描述写入目标纹理时使用的色彩空间和编码。
这可能导致超出 [0, 1] 范围的值被写入目标纹理(如果其格式可以表示这些值)。 否则,结果会被裁剪到目标纹理格式的允许范围内。
注意: 如果
colorSpace与源图像一致,则可能无需进行转换。详见 § 3.10.2 色彩空间转换省略。 premultipliedAlpha, 类型为 boolean,默认值为false-
描述写入目标纹理时,RGB 通道是否应由 alpha 通道进行预乘。
如果此选项为
true,且source也为预乘,则即使 RGB 分量超过其对应的 alpha 分量,也必须保留源 RGB 值。注意: 如果
premultipliedAlpha与源图像一致,则可能无需进行转换。详见 § 3.10.2 色彩空间转换省略。
11.2.5.
GPUCopyExternalImageSourceInfo
"GPUCopyExternalImageSourceInfo"
描述了 "copyExternalImageToTexture()" 操作中 "源(source)" 的
"参数信息(info)"。
typedef (ImageBitmap or ImageData or HTMLImageElement or HTMLVideoElement or VideoFrame or HTMLCanvasElement or OffscreenCanvas );GPUCopyExternalImageSource dictionary GPUCopyExternalImageSourceInfo {required GPUCopyExternalImageSource source ;GPUOrigin2D origin = {};boolean flipY =false ; };
GPUCopyExternalImageSourceInfo
有如下成员:
source,类型为 GPUCopyExternalImageSource-
纹素复制的源。 复制源数据会在调用
copyExternalImageToTexture()时被捕获。源尺寸由外部源尺寸表给出。 origin,类型为 GPUOrigin2D,默认值为{}-
定义复制起点——要复制的源子区域的最小(左上)角。与
copySize一起定义完整的复制区域。 flipY,类型为 boolean,默认值为false-
描述源图像是否需要垂直翻转。
如果该选项为
true,则复制时会进行垂直翻转:源区域的最后一行被复制到目标区域的第一行,依此类推。origin仍然以源图像的左上角为原点,向下递增。
当外部源被用来创建或复制到纹理时,外部源尺寸(external source dimensions) 由源类型决定,见下表:
11.2.6. 子程序(Subroutines)
参数:
-
GPUTexelCopyTextureInfotexelCopyTextureInfo
返回: GPUExtent3D
GPUTexelCopyTextureInfo 物理子资源尺寸的计算如下:
其 width、height 和 depthOrArrayLayers 分别为
texelCopyTextureInfo.texture
子资源(subresource) 在 mipmap 级别
texelCopyTextureInfo.mipLevel
的物理 mip 层特定纹理范围的宽度、高度和深度。
参数:
GPUTexelCopyBufferLayoutlayout-
线性纹理数据的布局。
GPUSize64byteSize-
线性数据的总字节数。
GPUTextureFormatformat-
纹理的格式。
GPUExtent3DcopyExtent-
要复制的纹理范围。
设备时间线步骤:
-
令:
-
若未满足下述输入校验要求则失败:
-
如果 heightInBlocks > 1, layout.
bytesPerRow必须被指定。 -
如果 copyExtent.depthOrArrayLayers > 1, layout.
bytesPerRow和 layout.rowsPerImage必须被指定。 -
如果指定,layout.
bytesPerRow必须 ≥ bytesInLastRow。 -
如果指定,layout.
rowsPerImage必须 ≥ heightInBlocks。
-
-
令:
-
bytesPerRow = layout.
bytesPerRow?? 0。 -
rowsPerImage = layout.
rowsPerImage?? 0。
注意: 这些默认值没有实际影响,因为总是被乘以 0。
-
-
令 requiredBytesInCopy = 0。
-
若 copyExtent.depthOrArrayLayers > 0:
-
requiredBytesInCopy 增加 bytesPerRow × rowsPerImage × (copyExtent.depthOrArrayLayers − 1)。
-
如果 heightInBlocks > 0:
-
requiredBytesInCopy 增加 bytesPerRow × (heightInBlocks − 1) + bytesInLastRow。
-
-
-
若未满足下述条件则失败:
-
布局应在线性数据范围内: layout.
offset+ requiredBytesInCopy ≤ byteSize。
-
参数:
GPUTexelCopyTextureInfotexelCopyTextureInfo-
要复制到的纹理子资源及复制起点。
GPUExtent3DcopySize-
纹理的尺寸。
设备时间线步骤:
-
令 subresourceSize = GPUTexelCopyTextureInfo 物理子资源尺寸。
-
返回下述所有条件是否满足:
-
(texelCopyTextureInfo.
origin.x + copySize.width) ≤ subresourceSize.width -
(texelCopyTextureInfo.
origin.y + copySize.height) ≤ subresourceSize.height -
(texelCopyTextureInfo.
origin.z + copySize.depthOrArrayLayers) ≤ subresourceSize.depthOrArrayLayers -
copySize.width 必须是 blockWidth 的倍数。
-
copySize.height 必须是 blockHeight 的倍数。
注意: 纹理复制范围针对 物理(向上取整)尺寸进行校验(对于压缩格式),允许复制操作访问未完全位于纹理内的纹理块。
-
GPUTextureFormat
format1 和 format2 当且仅当以下条件满足时为可复制兼容(copy-compatible):
-
format1 等于 format2,或
-
format1 和 format2 仅在是否为
srgb格式(即是否有-srgb后缀)上不同。
texture
的子资源子集,满足如下条件的每个子资源 s:
12. Command Buffers
Command buffers are pre-recorded lists of GPU commands (blocks of queue timeline
steps) that can be submitted to a GPUQueue for
execution.
Each GPU command
represents a task to be performed on the
queue timeline, such as
setting state, drawing, copying resources, etc.
A GPUCommandBuffer
can only be submitted once, at which point it becomes invalidated.
To reuse rendering commands across multiple submissions, use GPURenderBundle.
12.1. GPUCommandBuffer
[Exposed =(Window ,Worker ),SecureContext ]interface GPUCommandBuffer { };GPUCommandBuffer includes GPUObjectBase ;
GPUCommandBuffer
has the following device timeline properties:
[[command_list]], of type list<GPU command>, readonly-
A list of GPU commands to be executed on the Queue timeline when this command buffer is submitted.
[[renderState]], of type RenderState, initiallynull-
The current state used by any render pass commands being executed.
12.1.1. Command Buffer Creation
dictionary :GPUCommandBufferDescriptor GPUObjectDescriptorBase { };
13. Command Encoding
13.1. GPUCommandsMixin
GPUCommandsMixin
defines state common to all interfaces which encode commands.
It has no methods.
interface mixin GPUCommandsMixin { };
GPUCommandsMixin
has the following device timeline properties:
[[state]], of type encoder state, initially "open"-
The current state of the encoder.
[[commands]], of type list<GPU command>, initially[]-
A list of GPU commands to be executed on the Queue timeline when a
GPUCommandBuffercontaining these commands is submitted.
The encoder state may be one of the following:
- "open"
-
The encoder is available to encode new commands.
- "locked"
-
The encoder cannot be used, because it is locked by a child encoder: it is a
GPUCommandEncoder, and aGPURenderPassEncoderorGPUComputePassEncoderis active. The encoder becomes "open" again when the pass is ended.Any command issued in this state invalidates the encoder.
- "ended"
-
The encoder has been ended and new commands can no longer be encoded.
Any command issued in this state will generate a validation error.
GPUCommandsMixin
encoder run the following device timeline steps:
-
If encoder.
[[state]]is:- "open"
-
Return
true. - "locked"
-
Invalidate encoder and return
false. - "ended"
-
Generate a validation error, and return
false.
GPUCommandsMixin
encoder
which issues the steps of a GPU
Command command, run the following device timeline steps:
-
Append command to encoder.
[[commands]]. -
When command is executed as part of a
GPUCommandBuffer:-
Issue the steps of command.
-
13.2. GPUCommandEncoder
[Exposed =(Window ,Worker ),SecureContext ]interface GPUCommandEncoder {GPURenderPassEncoder beginRenderPass (GPURenderPassDescriptor descriptor );GPUComputePassEncoder beginComputePass (optional GPUComputePassDescriptor descriptor = {});undefined copyBufferToBuffer (GPUBuffer ,source GPUBuffer ,destination optional GPUSize64 );size undefined copyBufferToBuffer (GPUBuffer source ,GPUSize64 sourceOffset ,GPUBuffer destination ,GPUSize64 destinationOffset ,optional GPUSize64 size );undefined copyBufferToTexture (GPUTexelCopyBufferInfo source ,GPUTexelCopyTextureInfo destination ,GPUExtent3D copySize );undefined copyTextureToBuffer (GPUTexelCopyTextureInfo source ,GPUTexelCopyBufferInfo destination ,GPUExtent3D copySize );undefined copyTextureToTexture (GPUTexelCopyTextureInfo source ,GPUTexelCopyTextureInfo destination ,GPUExtent3D copySize );undefined clearBuffer (GPUBuffer buffer ,optional GPUSize64 offset = 0,optional GPUSize64 size );undefined resolveQuerySet (GPUQuerySet querySet ,GPUSize32 firstQuery ,GPUSize32 queryCount ,GPUBuffer destination ,GPUSize64 destinationOffset );GPUCommandBuffer finish (optional GPUCommandBufferDescriptor descriptor = {}); };GPUCommandEncoder includes GPUObjectBase ;GPUCommandEncoder includes GPUCommandsMixin ;GPUCommandEncoder includes GPUDebugCommandsMixin ;
13.2.1. Command Encoder Creation
dictionary :GPUCommandEncoderDescriptor GPUObjectDescriptorBase { };
createCommandEncoder(descriptor)-
Creates a
GPUCommandEncoder.Called on:GPUDevicethis.Arguments:
Arguments for the GPUDevice.createCommandEncoder(descriptor) method. Parameter Type Nullable Optional Description descriptorGPUCommandEncoderDescriptor✘ ✔ Description of the GPUCommandEncoderto create.Returns:
GPUCommandEncoderContent timeline steps:
-
Let e be ! create a new WebGPU object(this,
GPUCommandEncoder, descriptor). -
Issue the initialization steps on the Device timeline of this.
-
Return e.
Device timeline initialization steps:-
If any of the following conditions are unsatisfied generate a validation error, invalidate e and return.
-
this must not be lost.
-
-
GPUCommandEncoder,
encoding a command to clear a buffer, finishing the
encoder to get a GPUCommandBuffer,
then submitting it to the GPUQueue.
const commandEncoder= gpuDevice. createCommandEncoder(); commandEncoder. clearBuffer( buffer); const commandBuffer= commandEncoder. finish(); gpuDevice. queue. submit([ commandBuffer]);
13.3. Pass Encoding
beginRenderPass(descriptor)-
Begins encoding a render pass described by descriptor.
Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.beginRenderPass(descriptor) method. Parameter Type Nullable Optional Description descriptorGPURenderPassDescriptor✘ ✘ Description of the GPURenderPassEncoderto create.Returns:
GPURenderPassEncoderContent timeline steps:
-
For each non-
nullcolorAttachment in descriptor.colorAttachments:-
If colorAttachment.
clearValueis provided:-
? validate GPUColor shape(colorAttachment.
clearValue).
-
-
-
Let pass be a new
GPURenderPassEncoderobject. -
Issue the initialization steps on the Device timeline of this.
-
Return pass.
Device timeline initialization steps:-
Validate the encoder state of this. If it returns false, invalidate pass and return.
-
Let attachmentRegions be a list of [texture subresource,
depthSlice?] pairs, initially empty. Each pair describes the region of the texture to be rendered to, which includes a single depth slice for"3d"textures only. -
For each non-
nullcolorAttachment in descriptor.colorAttachments:-
Add [colorAttachment.
view, colorAttachment.depthSlice??null] to attachmentRegions. -
If colorAttachment.
resolveTargetis notnull:-
Add [colorAttachment.
resolveTarget,undefined] to attachmentRegions.
-
-
-
If any of the following requirements are unmet, invalidate pass and return.
-
descriptor must meet the Valid Usage rules given device this.
[[device]]. -
The set of texture regions in attachmentRegions must be pairwise disjoint. That is, no two texture regions may overlap.
-
-
Add each texture subresource in attachmentRegions to pass.
[[usage scope]]with usage attachment. -
Let depthStencilAttachment be descriptor.
depthStencilAttachment. -
If depthStencilAttachment is not
null:-
Let depthStencilView be depthStencilAttachment.
view. -
Add the depth subresource of depthStencilView, if any, to pass.
[[usage scope]]with usage attachment-read if depthStencilAttachment.depthReadOnlyis true, or attachment otherwise. -
Add the stencil subresource of depthStencilView, if any, to pass.
[[usage scope]]with usage attachment-read if depthStencilAttachment.stencilReadOnlyis true, or attachment otherwise. -
Set pass.
[[depthReadOnly]]to depthStencilAttachment.depthReadOnly. -
Set pass.
[[stencilReadOnly]]to depthStencilAttachment.stencilReadOnly.
-
-
Set pass.
[[layout]]to derive render targets layout from pass(descriptor). -
If descriptor.
timestampWritesis provided:-
Let timestampWrites be descriptor.
timestampWrites. -
If timestampWrites.
beginningOfPassWriteIndexis provided, append a GPU command to this.[[commands]]with the following steps:-
Before the pass commands begin executing, write the current queue timestamp into index timestampWrites.
beginningOfPassWriteIndexof timestampWrites.querySet.
-
-
If timestampWrites.
endOfPassWriteIndexis provided, set pass.[[endTimestampWrite]]to a GPU command with the following steps:-
After the pass commands finish executing, write the current queue timestamp into index timestampWrites.
endOfPassWriteIndexof timestampWrites.querySet.
-
-
-
Set pass.
[[drawCount]]to 0. -
Set pass.
[[maxDrawCount]]to descriptor.maxDrawCount. -
Set pass.
[[maxDrawCount]]to descriptor.maxDrawCount. -
Enqueue a command on this which issues the subsequent steps on the Queue timeline when executed.
Queue timeline steps:-
Let the
[[renderState]]of the currently executingGPUCommandBufferbe a new RenderState. -
Set
[[renderState]].[[colorAttachments]]to descriptor.colorAttachments. -
Set
[[renderState]].[[depthStencilAttachment]]to descriptor.depthStencilAttachment. -
For each non-
nullcolorAttachment in descriptor.colorAttachments:-
Let colorView be colorAttachment.
view. -
If colorView.
[[descriptor]].dimensionis:"3d"-
Let colorSubregion be colorAttachment.
depthSliceof colorView. - Otherwise
-
Let colorSubregion be colorView.
-
If colorAttachment.
loadOpis:"load"-
Ensure the contents of colorSubregion are loaded into the framebuffer memory associated with colorSubregion.
"clear"-
Set every texel of the framebuffer memory associated with colorSubregion to colorAttachment.
clearValue.
-
-
If depthStencilAttachment is not
null:-
If depthStencilAttachment.
depthLoadOpis:- Not provided
-
Assert that depthStencilAttachment.
depthReadOnlyistrueand ensure the contents of the depth subresource of depthStencilView are loaded into the framebuffer memory associated with depthStencilView. "load"-
Ensure the contents of the depth subresource of depthStencilView are loaded into the framebuffer memory associated with depthStencilView.
"clear"-
Set every texel of the framebuffer memory associated with the depth subresource of depthStencilView to depthStencilAttachment.
depthClearValue.
-
If depthStencilAttachment.
stencilLoadOpis:- Not provided
-
Assert that depthStencilAttachment.
stencilReadOnlyistrueand ensure the contents of the stencil subresource of depthStencilView are loaded into the framebuffer memory associated with depthStencilView. "load"-
Ensure the contents of the stencil subresource of depthStencilView are loaded into the framebuffer memory associated with depthStencilView.
"clear"-
Set every texel of the framebuffer memory associated with the stencil subresource depthStencilView to depthStencilAttachment.
stencilClearValue.
-
Note: Read-only depth-stencil attachments are implicitly treated as though the
"load"operation was used. Validation that requires the load op to not be provided for read-only attachments is done in GPURenderPassDepthStencilAttachment Valid Usage. -
beginComputePass(descriptor)-
Begins encoding a compute pass described by descriptor.
Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.beginComputePass(descriptor) method. Parameter Type Nullable Optional Description descriptorGPUComputePassDescriptor✘ ✔ Returns:
GPUComputePassEncoderContent timeline steps:
-
Let pass be a new
GPUComputePassEncoderobject. -
Issue the initialization steps on the Device timeline of this.
-
Return pass.
Device timeline initialization steps:-
Validate the encoder state of this. If it returns false, invalidate pass and return.
-
If any of the following requirements are unmet, invalidate pass and return.
-
If descriptor.
timestampWritesis provided:-
Validate timestampWrites(this.
[[device]], descriptor.timestampWrites) must return true.
-
-
-
If descriptor.
timestampWritesis provided:-
Let timestampWrites be descriptor.
timestampWrites. -
If timestampWrites.
beginningOfPassWriteIndexis provided, append a GPU command to this.[[commands]]with the following steps:-
Before the pass commands begin executing, write the current queue timestamp into index timestampWrites.
beginningOfPassWriteIndexof timestampWrites.querySet.
-
-
If timestampWrites.
endOfPassWriteIndexis provided, set pass.[[endTimestampWrite]]to a GPU command with the following steps:-
After the pass commands finish executing, write the current queue timestamp into index timestampWrites.
endOfPassWriteIndexof timestampWrites.querySet.
-
-
-
13.4. Buffer Copy Commands
copyBufferToBuffer() has two overloads:
copyBufferToBuffer(source, destination, size)-
Shorthand, equivalent to
copyBufferToBuffer(source, 0, destination, 0, size). copyBufferToBuffer(source, sourceOffset, destination, destinationOffset, size)-
Encode a command into the
GPUCommandEncoderthat copies data from a sub-region of aGPUBufferto a sub-region of anotherGPUBuffer.Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.copyBufferToBuffer(source, sourceOffset, destination, destinationOffset, size) method. Parameter Type Nullable Optional Description sourceGPUBuffer✘ ✘ The GPUBufferto copy from.sourceOffsetGPUSize64✘ ✘ Offset in bytes into source to begin copying from. destinationGPUBuffer✘ ✘ The GPUBufferto copy to.destinationOffsetGPUSize64✘ ✘ Offset in bytes into destination to place the copied data. sizeGPUSize64✘ ✔ Bytes to copy. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If size is
undefined, set it to source.size− sourceOffset. -
If any of the following conditions are unsatisfied, invalidate this and return.
-
source is valid to use with this.
-
destination is valid to use with this.
-
size is a multiple of 4.
-
sourceOffset is a multiple of 4.
-
destinationOffset is a multiple of 4.
-
source.
size≥ (sourceOffset + size). -
destination.
size≥ (destinationOffset + size). -
source and destination are not the same
GPUBuffer.
-
-
Enqueue a command on this which issues the subsequent steps on the Queue timeline when executed.
Queue timeline steps:-
Copy size bytes of source, beginning at sourceOffset, into destination, beginning at destinationOffset.
-
clearBuffer(buffer, offset, size)-
Encode a command into the
GPUCommandEncoderthat fills a sub-region of aGPUBufferwith zeros.Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.clearBuffer(buffer, offset, size) method. Parameter Type Nullable Optional Description bufferGPUBuffer✘ ✘ The GPUBufferto clear.offsetGPUSize64✘ ✔ Offset in bytes into buffer where the sub-region to clear begins. sizeGPUSize64✘ ✔ Size in bytes of the sub-region to clear. Defaults to the size of the buffer minus offset. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If size is missing, set size to
max(0, buffer..size- offset) -
If any of the following conditions are unsatisfied, invalidate this and return.
-
buffer is valid to use with this.
-
size is a multiple of 4.
-
offset is a multiple of 4.
-
buffer.
size≥ (offset + size).
-
-
Enqueue a command on this which issues the subsequent steps on the Queue timeline when executed.
Queue timeline steps:-
Set size bytes of buffer to
0starting at offset.
-
13.5. Texel Copy Commands
copyBufferToTexture(source, destination, copySize)-
Encode a command into the
GPUCommandEncoderthat copies data from a sub-region of aGPUBufferto a sub-region of one or multiple continuous texture subresources.Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.copyBufferToTexture(source, destination, copySize) method. Parameter Type Nullable Optional Description sourceGPUTexelCopyBufferInfo✘ ✘ Combined with copySize, defines the region of the source buffer. destinationGPUTexelCopyTextureInfo✘ ✘ Combined with copySize, defines the region of the destination texture subresource. copySizeGPUExtent3D✘ ✘ Returns:
undefinedContent timeline steps:
-
? validate GPUOrigin3D shape(destination.
origin). -
? validate GPUExtent3D shape(copySize).
-
Issue the subsequent steps on the Device timeline of this.
[[device]]:
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Let aligned be
true. -
If any of the following conditions are unsatisfied, invalidate this and return.
-
validating GPUTexelCopyBufferInfo(source) returns
true. -
validating texture buffer copy(destination, source, dataLength, copySize,
COPY_DST, aligned) returnstrue.
-
-
Enqueue a command on this which issues the subsequent steps on the Queue timeline when executed.
Queue timeline steps:-
Let blockWidth be the texel block width of destination.
texture. -
Let blockHeight be the texel block height of destination.
texture. -
Let dstOrigin be destination.
origin. -
Let dstBlockOriginX be (dstOrigin.x ÷ blockWidth).
-
Let dstBlockOriginY be (dstOrigin.y ÷ blockHeight).
-
Let blockColumns be (copySize.width ÷ blockWidth).
-
Let blockRows be (copySize.height ÷ blockHeight).
-
Assert that dstBlockOriginX, dstBlockOriginY, blockColumns, and blockRows are integers.
-
For each z in the range [0, copySize.depthOrArrayLayers − 1]:
-
Let dstSubregion be texture copy sub-region (z + dstOrigin.z) of destination.
-
For each y in the range [0, blockRows − 1]:
-
For each x in the range [0, blockColumns − 1]:
-
Let blockOffset be the texel block byte offset of source for (x, y, z) of destination.
texture. -
Set texel block (dstBlockOriginX + x, dstBlockOriginY + y) of dstSubregion to be an equivalent texel representation to the texel block described by source.
bufferat offset blockOffset.
-
-
-
-
copyTextureToBuffer(source, destination, copySize)-
Encode a command into the
GPUCommandEncoderthat copies data from a sub-region of one or multiple continuous texture subresources to a sub-region of aGPUBuffer.Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.copyTextureToBuffer(source, destination, copySize) method. Parameter Type Nullable Optional Description sourceGPUTexelCopyTextureInfo✘ ✘ Combined with copySize, defines the region of the source texture subresources. destinationGPUTexelCopyBufferInfo✘ ✘ Combined with copySize, defines the region of the destination buffer. copySizeGPUExtent3D✘ ✘ Returns:
undefinedContent timeline steps:
-
? validate GPUOrigin3D shape(source.
origin). -
? validate GPUExtent3D shape(copySize).
-
Issue the subsequent steps on the Device timeline of this.
[[device]]:
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Let aligned be
true. -
If any of the following conditions are unsatisfied, invalidate this and return.
-
validating GPUTexelCopyBufferInfo(destination) returns
true. -
validating texture buffer copy(source, destination, dataLength, copySize,
COPY_SRC, aligned) returnstrue.
-
-
Enqueue a command on this which issues the subsequent steps on the Queue timeline when executed.
Queue timeline steps:-
Let blockWidth be the texel block width of source.
texture. -
Let blockHeight be the texel block height of source.
texture. -
Let srcOrigin be source.
origin. -
Let srcBlockOriginX be (srcOrigin.x ÷ blockWidth).
-
Let srcBlockOriginY be (srcOrigin.y ÷ blockHeight).
-
Let blockColumns be (copySize.width ÷ blockWidth).
-
Let blockRows be (copySize.height ÷ blockHeight).
-
Assert that srcBlockOriginX, srcBlockOriginY, blockColumns, and blockRows are integers.
-
For each z in the range [0, copySize.depthOrArrayLayers − 1]:
-
Let srcSubregion be texture copy sub-region (z + srcOrigin.z) of source.
-
For each y in the range [0, blockRows − 1]:
-
For each x in the range [0, blockColumns − 1]:
-
Let blockOffset be the texel block byte offset of destination for (x, y, z) of source.
texture. -
Set destination.
bufferat offset blockOffset to be an equivalent texel representation to texel block (srcBlockOriginX + x, srcBlockOriginY + y) of srcSubregion.
-
-
-
-
copyTextureToTexture(source, destination, copySize)-
Encode a command into the
GPUCommandEncoderthat copies data from a sub-region of one or multiple contiguous texture subresources to another sub-region of one or multiple continuous texture subresources.Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.copyTextureToTexture(source, destination, copySize) method. Parameter Type Nullable Optional Description sourceGPUTexelCopyTextureInfo✘ ✘ Combined with copySize, defines the region of the source texture subresources. destinationGPUTexelCopyTextureInfo✘ ✘ Combined with copySize, defines the region of the destination texture subresources. copySizeGPUExtent3D✘ ✘ Returns:
undefinedContent timeline steps:
-
? validate GPUOrigin3D shape(source.
origin). -
? validate GPUOrigin3D shape(destination.
origin). -
? validate GPUExtent3D shape(copySize).
-
Issue the subsequent steps on the Device timeline of this.
[[device]]:
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
Let srcTexture be source.
texture. -
Let dstTexture be destination.
texture. -
validating GPUTexelCopyTextureInfo(source, copySize) returns
true. -
validating GPUTexelCopyTextureInfo(destination, copySize) returns
true. -
srcTexture.
sampleCountis equal to dstTexture.sampleCount. -
srcTexture.
formatand dstTexture.formatmust be copy-compatible. -
If srcTexture.
formatis a depth-stencil format: -
The set of subresources for texture copy(source, copySize) and the set of subresources for texture copy(destination, copySize) are disjoint.
-
-
Enqueue a command on this which issues the subsequent steps on the Queue timeline when executed.
Queue timeline steps:-
Let blockWidth be the texel block width of source.
texture. -
Let blockHeight be the texel block height of source.
texture. -
Let srcOrigin be source.
origin. -
Let srcBlockOriginX be (srcOrigin.x ÷ blockWidth).
-
Let srcBlockOriginY be (srcOrigin.y ÷ blockHeight).
-
Let dstOrigin be destination.
origin. -
Let dstBlockOriginX be (dstOrigin.x ÷ blockWidth).
-
Let dstBlockOriginY be (dstOrigin.y ÷ blockHeight).
-
Let blockColumns be (copySize.width ÷ blockWidth).
-
Let blockRows be (copySize.height ÷ blockHeight).
-
Assert that srcBlockOriginX, srcBlockOriginY, dstBlockOriginX, dstBlockOriginY, blockColumns, and blockRows are integers.
-
For each z in the range [0, copySize.depthOrArrayLayers − 1]:
-
Let srcSubregion be texture copy sub-region (z + srcOrigin.z) of source.
-
Let dstSubregion be texture copy sub-region (z + dstOrigin.z) of destination.
-
For each y in the range [0, blockRows − 1]:
-
For each x in the range [0, blockColumns − 1]:
-
Set texel block (dstBlockOriginX + x, dstBlockOriginY + y) of dstSubregion to be an equivalent texel representation to texel block (srcBlockOriginX + x, srcBlockOriginY + y) of srcSubregion.
-
-
-
-
13.6. Queries
resolveQuerySet(querySet, firstQuery, queryCount, destination, destinationOffset)-
Resolves query results from a
GPUQuerySetout into a range of aGPUBuffer.Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.resolveQuerySet(querySet, firstQuery, queryCount, destination, destinationOffset) method. Parameter Type Nullable Optional Description querySetGPUQuerySet✘ ✘ firstQueryGPUSize32✘ ✘ queryCountGPUSize32✘ ✘ destinationGPUBuffer✘ ✘ destinationOffsetGPUSize64✘ ✘ Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
querySet is valid to use with this.
-
destination is valid to use with this.
-
destination.
usagecontainsQUERY_RESOLVE. -
firstQuery < the number of queries in querySet.
-
(firstQuery + queryCount) ≤ the number of queries in querySet.
-
destinationOffset is a multiple of 256.
-
destinationOffset + 8 × queryCount ≤ destination.
size.
-
-
Enqueue a command on this which issues the subsequent steps on the Queue timeline when executed.
Queue timeline steps:-
Let queryIndex be firstQuery.
-
Let offset be destinationOffset.
-
While queryIndex < firstQuery + queryCount:
-
Set 8 bytes of destination, beginning at offset, to be the value of querySet at queryIndex.
-
Set queryIndex to be queryIndex + 1.
-
Set offset to be offset + 8.
-
-
13.7. Finalization
A GPUCommandBuffer
containing the commands recorded by the GPUCommandEncoder
can be created
by calling finish().
Once finish()
has been called the
command encoder can no longer be used.
finish(descriptor)-
Completes recording of the commands sequence and returns a corresponding
GPUCommandBuffer.Called on:GPUCommandEncoderthis.Arguments:
Arguments for the GPUCommandEncoder.finish(descriptor) method. Parameter Type Nullable Optional Description descriptorGPUCommandBufferDescriptor✘ ✔ Returns:
GPUCommandBufferContent timeline steps:
-
Let commandBuffer be a new
GPUCommandBuffer. -
Issue the finish steps on the Device timeline of this.
[[device]]. -
Return commandBuffer.
Device timeline finish steps:-
Let validationSucceeded be
trueif all of the following requirements are met, andfalseotherwise.-
this must be valid.
-
this.
[[debug_group_stack]]must be empty.
-
-
If validationSucceeded is
false, then:-
Return an invalidated
GPUCommandBuffer.
-
Set commandBuffer.
[[command_list]]to this.[[commands]].
-
14. Programmable Passes
interface mixin {GPUBindingCommandsMixin undefined setBindGroup (GPUIndex32 index ,GPUBindGroup ?bindGroup ,optional sequence <GPUBufferDynamicOffset >dynamicOffsets = []);undefined setBindGroup (GPUIndex32 index ,GPUBindGroup ?bindGroup , [AllowShared ]Uint32Array dynamicOffsetsData ,GPUSize64 dynamicOffsetsDataStart ,GPUSize32 dynamicOffsetsDataLength ); };
GPUBindingCommandsMixin
assumes the presence of
GPUObjectBase
and GPUCommandsMixin
members on the same object.
It must only be included by interfaces which also include those mixins.
GPUBindingCommandsMixin
has the following device timeline properties:
[[bind_groups]], of type ordered map<GPUIndex32,GPUBindGroup>, initially empty-
The current
GPUBindGroupfor each index. [[dynamic_offsets]], of type ordered map<GPUIndex32, list<GPUBufferDynamicOffset>>, initally empty-
The current dynamic offsets for each
[[bind_groups]]entry.
14.1. Bind Groups
setBindGroup() has two overloads:
setBindGroup(index, bindGroup, dynamicOffsets)-
Sets the current
GPUBindGroupfor the given index.Called on:GPUBindingCommandsMixinthis.Arguments:
index, of typeGPUIndex32, non-nullable, required-
The index to set the bind group at.
bindGroup, of typeGPUBindGroup, nullable, required-
Bind group to use for subsequent render or compute commands.
dynamicOffsets, of type sequence<GPUBufferDynamicOffset>, non-nullable, defaulting to[]-
Array containing buffer offsets in bytes for each entry in bindGroup marked as
buffer.hasDynamicOffset, ordered byGPUBindGroupLayoutEntry.binding. See note for additional details.
Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Let dynamicOffsetCount be 0 if
bindGroupisnull, or bindGroup.[[layout]].[[dynamicOffsetCount]]if not. -
If any of the following requirements are unmet, invalidate this and return.
-
index must be < this.
[[device]].[[limits]].maxBindGroups. -
dynamicOffsets.size must equal dynamicOffsetCount.
-
-
If bindGroup is
null:-
Remove this.
[[bind_groups]][index]. -
Remove this.
[[dynamic_offsets]][index].
Otherwise:
-
If any of the following requirements are unmet, invalidate this and return.
-
bindGroup must be valid to use with this.
-
For each dynamic binding (bufferBinding, bufferLayout, dynamicOffsetIndex) in bindGroup:
-
bufferBinding.
offset+ dynamicOffsets[dynamicOffsetIndex] + bufferLayout.minBindingSizemust be ≤ bufferBinding.buffer.size. -
If bufferLayout.
typeis"uniform":-
dynamicOffset must be a multiple of
minUniformBufferOffsetAlignment.
-
-
If bufferLayout.
typeis"storage"or"read-only-storage":-
dynamicOffset must be a multiple of
minStorageBufferOffsetAlignment.
-
-
-
-
Set this.
[[bind_groups]][index] to be bindGroup. -
Set this.
[[dynamic_offsets]][index] to be a copy of dynamicOffsets. -
If this is a
GPURenderCommandsMixin:-
For each bindGroup in this.
[[bind_groups]], merge bindGroup.[[usedResources]]into this.[[usage scope]]
-
-
setBindGroup(index, bindGroup, dynamicOffsetsData, dynamicOffsetsDataStart, dynamicOffsetsDataLength)-
Sets the current
GPUBindGroupfor the given index, specifying dynamic offsets as a subset of aUint32Array.Called on:GPUBindingCommandsMixinthis.Arguments:
Arguments for the GPUBindingCommandsMixin.setBindGroup(index, bindGroup, dynamicOffsetsData, dynamicOffsetsDataStart, dynamicOffsetsDataLength) method. Parameter Type Nullable Optional Description indexGPUIndex32✘ ✘ The index to set the bind group at. bindGroupGPUBindGroup?✔ ✘ Bind group to use for subsequent render or compute commands. dynamicOffsetsDataUint32Array✘ ✘ Array containing buffer offsets in bytes for each entry in bindGroup marked as buffer.hasDynamicOffset, ordered byGPUBindGroupLayoutEntry.binding. See note for additional details.dynamicOffsetsDataStartGPUSize64✘ ✘ Offset in elements into dynamicOffsetsData where the buffer offset data begins. dynamicOffsetsDataLengthGPUSize32✘ ✘ Number of buffer offsets to read from dynamicOffsetsData. Returns:
undefinedContent timeline steps:
-
If any of the following requirements are unmet, throw a
RangeErrorand return.-
dynamicOffsetsDataStart must be ≥ 0.
-
dynamicOffsetsDataStart + dynamicOffsetsDataLength must be ≤ dynamicOffsetsData.
length.
-
-
Let dynamicOffsets be a list containing the range, starting at index dynamicOffsetsDataStart, of dynamicOffsetsDataLength elements of a copy of dynamicOffsetsData.
-
Call this.
setBindGroup(index, bindGroup, dynamicOffsets).
-
GPUBindGroupLayoutEntry.binding
order.
This means that if dynamic bindings is the list of each GPUBindGroupLayoutEntry
in the GPUBindGroupLayout
with buffer?.hasDynamicOffset
set to true, sorted by
GPUBindGroupLayoutEntry.binding,
then dynamic offset[i], as supplied to
setBindGroup(), will correspond to
dynamic bindings[i].
GPUBindGroupLayout
created with the following call:
// Note the bindings are listed out-of-order in this array, but it // doesn’t matter because they will be sorted by binding index. let layout= gpuDevice. createBindGroupLayout({ entries: [{ binding: 1 , buffer: {}, }, { binding: 2 , buffer: { dynamicOffset: true }, }, { binding: 0 , buffer: { dynamicOffset: true }, }] });
Used by a GPUBindGroup
created with the following call:
// Like above, the array order doesn’t matter here. // It doesn’t even need to match the order used in the layout. let bindGroup= gpuDevice. createBindGroup({ layout: layout, entries: [{ binding: 1 , resource: { buffer: bufferA, offset: 256 }, }, { binding: 2 , resource: { buffer: bufferB, offset: 512 }, }, { binding: 0 , resource: { buffer: bufferC}, }] });
And bound with the following call:
pass. setBindGroup( 0 , bindGroup, [ 1024 , 2048 ]);
The following buffer offsets will be applied:
| Binding | Buffer | Offset |
|---|---|---|
| 0 | bufferC | 1024 (Dynamic) |
| 1 | bufferA | 256 (Static) |
| 2 | bufferB | 2560 (Static + Dynamic) |
GPUBindGroup
bindGroup
with a given list of steps to be executed for each dynamic offset, run the following device timeline steps:
-
Let dynamicOffsetIndex be
0. -
Let layout be bindGroup.
[[layout]]. -
For each
GPUBindGroupEntryentry in bindGroup.[[entries]]ordered in increasing values of entry.binding:-
Let bindingDescriptor be the
GPUBindGroupLayoutEntryat layout.[[entryMap]][entry.binding]: -
If bindingDescriptor.
buffer?.hasDynamicOffsetistrue:-
Let bufferBinding be get as buffer binding(entry.
resource). -
Let bufferLayout be bindingDescriptor.
buffer. -
Call steps with bufferBinding, bufferLayout, and dynamicOffsetIndex.
-
Let dynamicOffsetIndex be dynamicOffsetIndex +
1
-
-
Arguments:
GPUBindingCommandsMixinencoder-
Encoder whose bind groups are being validated.
GPUPipelineBasepipeline-
Pipeline to validate encoders bind groups are compatible with.
Device timeline steps:
-
If any of the following conditions are unsatisfied, return
false:-
pipeline must not be
null. -
All bind groups used by the pipeline must be set and compatible with the pipeline layout: For each pair of (
GPUIndex32index,GPUBindGroupLayoutbindGroupLayout) in pipeline.[[layout]].[[bindGroupLayouts]]:-
If bindGroupLayout is
null, continue. -
Let bindGroup be encoder.
[[bind_groups]][index]. -
Let dynamicOffsets be encoder.
[[dynamic_offsets]][index]. -
bindGroup must not be
null. -
bindGroup.
[[layout]]must be group-equivalent with bindGroupLayout. -
Let dynamicOffsetIndex be 0.
-
For each
GPUBindGroupEntrybindGroupEntry in bindGroup.[[entries]], sorted by bindGroupEntry.binding:-
Let bindGroupLayoutEntry be bindGroup.
[[layout]].[[entryMap]][bindGroupEntry.binding]. -
Let bound be get as buffer binding(bindGroupEntry.
resource). -
If bindGroupLayoutEntry.
buffer.hasDynamicOffset:-
Increment bound.
offsetby dynamicOffsets[dynamicOffsetIndex]. -
Increment dynamicOffsetIndex by 1.
-
-
If bindGroupEntry.
[[prevalidatedSize]]isfalse:-
effective buffer binding size(bound) must be ≥ minimum buffer binding size of the binding variable in pipeline’s shader that corresponds to bindGroupEntry.
-
-
-
-
Encoder bind groups alias a writable resource(encoder, pipeline) must be
false.
-
Otherwise return true.
GPUTextureView
object).
Note: This algorithm limits the use of the usage scope storage exception.
Arguments:
GPUBindingCommandsMixinencoder-
Encoder whose bind groups are being validated.
GPUPipelineBasepipeline-
Pipeline to validate encoders bind groups are compatible with.
Device timeline steps:
-
For each stage in [
VERTEX,FRAGMENT,COMPUTE]:-
Let bufferBindings be a list of (
GPUBufferBinding,boolean) pairs, where the latter indicates whether the resource was used as writable. -
Let textureViews be a list of (
GPUTextureView,boolean) pairs, where the latter indicates whether the resource was used as writable. -
For each pair of (
GPUIndex32bindGroupIndex,GPUBindGroupLayoutbindGroupLayout) in pipeline.[[layout]].[[bindGroupLayouts]]:-
Let bindGroup be encoder.
[[bind_groups]][bindGroupIndex]. -
Let bindGroupLayoutEntries be bindGroupLayout.
[[descriptor]].entries. -
Let bufferRanges be the bound buffer ranges of bindGroup, given dynamic offsets encoder.
[[dynamic_offsets]][bindGroupIndex] -
For each (
GPUBindGroupLayoutEntrybindGroupLayoutEntry,GPUBufferBindingresource) in bufferRanges, in which bindGroupLayoutEntry.visibilitycontains stage:-
Let resourceWritable be (bindGroupLayoutEntry.
buffer.type=="storage"). -
For each pair (
GPUBufferBindingpastResource,booleanpastResourceWritable) in bufferBindings:-
If (resourceWritable or pastResourceWritable) is true, and pastResource and resource are buffer-binding-aliasing, return
true.
-
-
Append (resource, resourceWritable) to bufferBindings.
-
-
For each
GPUBindGroupLayoutEntrybindGroupLayoutEntry in bindGroupLayoutEntries, and correspondingGPUTextureViewresource in bindGroup, in which bindGroupLayoutEntry.visibilitycontains stage:-
If bindGroupLayoutEntry.
storageTextureis not provided, continue. -
Let resourceWritable be whether bindGroupLayoutEntry.
storageTexture.accessis a writable access mode. -
For each pair (
GPUTextureViewpastResource,booleanpastResourceWritable) in textureViews,-
If (resourceWritable or pastResourceWritable) is true, and pastResource and resource is texture-view-aliasing, return
true.
-
-
Append (resource, resourceWritable) to textureViews.
-
-
-
-
Return
false.
Note: Implementations are strongly encouraged to optimize this algorithm.
15. Debug Markers
GPUDebugCommandsMixin provides methods to apply debug
labels to groups
of commands or insert a single label into the command sequence.
Debug groups can be nested to create a hierarchy of labeled commands, and must be well-balanced.
Like object labels,
these labels have no required behavior, but may be shown
in error messages and browser developer tools, and may be passed to native API backends.
interface mixin GPUDebugCommandsMixin {undefined pushDebugGroup (USVString groupLabel );undefined popDebugGroup ();undefined insertDebugMarker (USVString markerLabel ); };
GPUDebugCommandsMixin
assumes the presence of
GPUObjectBase
and GPUCommandsMixin
members on the same object.
It must only be included by interfaces which also include those mixins.
GPUDebugCommandsMixin
has the following device timeline properties:
[[debug_group_stack]], of type stack<USVString>-
A stack of active debug group labels.
GPUDebugCommandsMixin
has the following methods:
pushDebugGroup(groupLabel)-
Begins a labeled debug group containing subsequent commands.
Called on:GPUDebugCommandsMixinthis.Arguments:
Arguments for the GPUDebugCommandsMixin.pushDebugGroup(groupLabel) method. Parameter Type Nullable Optional Description groupLabelUSVString✘ ✘ The label for the command group. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Push groupLabel onto this.
[[debug_group_stack]].
-
popDebugGroup()-
Ends the labeled debug group most recently started by
pushDebugGroup().Called on:GPUDebugCommandsMixinthis.Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following requirements are unmet, invalidate this and return.
-
this.
[[debug_group_stack]]must not be empty.
-
-
Pop an entry off of this.
[[debug_group_stack]].
-
insertDebugMarker(markerLabel)-
Marks a point in a stream of commands with a label.
Called on:GPUDebugCommandsMixinthis.Arguments:
Arguments for the GPUDebugCommandsMixin.insertDebugMarker(markerLabel) method. Parameter Type Nullable Optional Description markerLabelUSVString✘ ✘ The label to insert. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
16. Compute Passes
16.1. GPUComputePassEncoder
[Exposed =(Window ,Worker ),SecureContext ]interface GPUComputePassEncoder {undefined setPipeline (GPUComputePipeline pipeline );undefined dispatchWorkgroups (GPUSize32 workgroupCountX ,optional GPUSize32 workgroupCountY = 1,optional GPUSize32 workgroupCountZ = 1);undefined dispatchWorkgroupsIndirect (GPUBuffer indirectBuffer ,GPUSize64 indirectOffset );undefined end (); };GPUComputePassEncoder includes GPUObjectBase ;GPUComputePassEncoder includes GPUCommandsMixin ;GPUComputePassEncoder includes GPUDebugCommandsMixin ;GPUComputePassEncoder includes GPUBindingCommandsMixin ;
GPUComputePassEncoder
has the following device timeline properties:
[[command_encoder]], of typeGPUCommandEncoder, readonly-
The
GPUCommandEncoderthat created this compute pass encoder. [[endTimestampWrite]], of type GPU command?, readonly, defaulting tonull-
GPU command, if any, writing a timestamp when the pass ends.
[[pipeline]], of typeGPUComputePipeline, initiallynull-
The current
GPUComputePipeline.
16.1.1. Compute Pass Encoder Creation
dictionary {GPUComputePassTimestampWrites required GPUQuerySet querySet ;GPUSize32 beginningOfPassWriteIndex ;GPUSize32 endOfPassWriteIndex ; };
querySet, of type GPUQuerySet-
The
GPUQuerySet, of type"timestamp", that the query results will be written to. beginningOfPassWriteIndex, of type GPUSize32-
If defined, indicates the query index in
querySetinto which the timestamp at the beginning of the compute pass will be written. endOfPassWriteIndex, of type GPUSize32-
If defined, indicates the query index in
querySetinto which the timestamp at the end of the compute pass will be written.
Note: Timestamp query values are written in nanoseconds, but how the value is determined is implementation-defined and may not increase monotonically. See § 20.4 Timestamp Query for details.
dictionary :GPUComputePassDescriptor GPUObjectDescriptorBase {GPUComputePassTimestampWrites timestampWrites ; };
timestampWrites, of type GPUComputePassTimestampWrites-
Defines which timestamp values will be written for this pass, and where to write them to.
16.1.2. Dispatch
setPipeline(pipeline)-
Sets the current
GPUComputePipeline.Called on:GPUComputePassEncoderthis.Arguments:
Arguments for the GPUComputePassEncoder.setPipeline(pipeline) method. Parameter Type Nullable Optional Description pipelineGPUComputePipeline✘ ✘ The compute pipeline to use for subsequent dispatch commands. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
pipeline is valid to use with this.
-
-
Set this.
[[pipeline]]to be pipeline.
-
dispatchWorkgroups(workgroupCountX, workgroupCountY, workgroupCountZ)-
Dispatch work to be performed with the current
GPUComputePipeline. See § 23.1 Computing for the detailed specification.Called on:GPUComputePassEncoderthis.Arguments:
Arguments for the GPUComputePassEncoder.dispatchWorkgroups(workgroupCountX, workgroupCountY, workgroupCountZ) method. Parameter Type Nullable Optional Description workgroupCountXGPUSize32✘ ✘ X dimension of the grid of workgroups to dispatch. workgroupCountYGPUSize32✘ ✔ Y dimension of the grid of workgroups to dispatch. workgroupCountZGPUSize32✘ ✔ Z dimension of the grid of workgroups to dispatch. NOTE:Thex,y, andzvalues passed todispatchWorkgroups()anddispatchWorkgroupsIndirect()are the number of workgroups to dispatch for each dimension, not the number of shader invocations to perform across each dimension. This matches the behavior of modern native GPU APIs, but differs from the behavior of OpenCL.This means that if a
GPUShaderModuledefines an entry point with@workgroup_size(4, 4), and work is dispatched to it with the callcomputePass.dispatchWorkgroups(8, 8);the entry point will be invoked 1024 times total: Dispatching a 4x4 workgroup 8 times along both the X and Y axes. (4*4*8*8=1024)Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Let usageScope be an empty usage scope.
-
For each bindGroup in this.
[[bind_groups]], merge bindGroup.[[usedResources]]into this.[[usage scope]] -
If any of the following conditions are unsatisfied, invalidate this and return.
-
usageScope must satisfy usage scope validation.
-
Validate encoder bind groups(this, this.
[[pipeline]]) istrue. -
all of workgroupCountX, workgroupCountY and workgroupCountZ are ≤ this.device.limits.
maxComputeWorkgroupsPerDimension.
-
-
Let bindingState be a snapshot of this’s current state.
-
Enqueue a command on this which issues the subsequent steps on the Queue timeline.
Queue timeline steps:-
Execute a grid of workgroups with dimensions [workgroupCountX, workgroupCountY, workgroupCountZ] with bindingState.
[[pipeline]]using bindingState.[[bind_groups]].
-
dispatchWorkgroupsIndirect(indirectBuffer, indirectOffset)-
Dispatch work to be performed with the current
GPUComputePipelineusing parameters read from aGPUBuffer. See § 23.1 Computing for the detailed specification.The indirect dispatch parameters encoded in the buffer must be a tightly packed block of three 32-bit unsigned integer values (12 bytes total), given in the same order as the arguments for
dispatchWorkgroups(). For example:let dispatchIndirectParameters= new Uint32Array( 3 ); dispatchIndirectParameters[ 0 ] = workgroupCountX; dispatchIndirectParameters[ 1 ] = workgroupCountY; dispatchIndirectParameters[ 2 ] = workgroupCountZ; Called on:GPUComputePassEncoderthis.Arguments:
Arguments for the GPUComputePassEncoder.dispatchWorkgroupsIndirect(indirectBuffer, indirectOffset) method. Parameter Type Nullable Optional Description indirectBufferGPUBuffer✘ ✘ Buffer containing the indirect dispatch parameters. indirectOffsetGPUSize64✘ ✘ Offset in bytes into indirectBuffer where the dispatch data begins. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Let usageScope be an empty usage scope.
-
For each bindGroup in this.
[[bind_groups]], merge bindGroup.[[usedResources]]into this.[[usage scope]] -
If any of the following conditions are unsatisfied, invalidate this and return.
-
usageScope must satisfy usage scope validation.
-
Validate encoder bind groups(this, this.
[[pipeline]]) istrue. -
indirectBuffer is valid to use with this.
-
indirectOffset + sizeof(indirect dispatch parameters) ≤ indirectBuffer.
size. -
indirectOffset is a multiple of 4.
-
-
Let bindingState be a snapshot of this’s current state.
-
Enqueue a command on this which issues the subsequent steps on the Queue timeline.
Queue timeline steps:-
Let workgroupCountX be an unsigned 32-bit integer read from indirectBuffer at indirectOffset bytes.
-
Let workgroupCountY be an unsigned 32-bit integer read from indirectBuffer at (indirectOffset + 4) bytes.
-
Let workgroupCountZ be an unsigned 32-bit integer read from indirectBuffer at (indirectOffset + 8) bytes.
-
If workgroupCountX, workgroupCountY, or workgroupCountZ is greater than this.device.limits.
maxComputeWorkgroupsPerDimension, return. -
Execute a grid of workgroups with dimensions [workgroupCountX, workgroupCountY, workgroupCountZ] with bindingState.
[[pipeline]]using bindingState.[[bind_groups]].
-
16.1.3. Finalization
The compute pass encoder can be ended by calling end()
once the user
has finished recording commands for the pass. Once end()
has been
called the compute pass encoder can no longer be used.
end()-
Completes recording of the compute pass commands sequence.
Called on:GPUComputePassEncoderthis.Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Let parentEncoder be this.
[[command_encoder]]. -
If any of the following requirements are unmet, generate a validation error and return.
-
If any of the following requirements are unmet, invalidate parentEncoder and return.
-
this must be valid.
-
this.
[[debug_group_stack]]must be empty.
-
-
Extend parentEncoder.
[[commands]]with this.[[commands]]. -
If this.
[[endTimestampWrite]]is notnull:-
Extend parentEncoder.
[[commands]]with this.[[endTimestampWrite]].
-
-
17. Render Passes
17.1. GPURenderPassEncoder
[Exposed =(Window ,Worker ),SecureContext ]interface GPURenderPassEncoder {undefined setViewport (float x ,float y ,float width ,float height ,float minDepth ,float maxDepth );undefined setScissorRect (GPUIntegerCoordinate x ,GPUIntegerCoordinate y ,GPUIntegerCoordinate width ,GPUIntegerCoordinate height );undefined setBlendConstant (GPUColor color );undefined setStencilReference (GPUStencilValue reference );undefined beginOcclusionQuery (GPUSize32 queryIndex );undefined endOcclusionQuery ();undefined executeBundles (sequence <GPURenderBundle >bundles );undefined end (); };GPURenderPassEncoder includes GPUObjectBase ;GPURenderPassEncoder includes GPUCommandsMixin ;GPURenderPassEncoder includes GPUDebugCommandsMixin ;GPURenderPassEncoder includes GPUBindingCommandsMixin ;GPURenderPassEncoder includes GPURenderCommandsMixin ;
GPURenderPassEncoder
has the following device timeline properties:
[[command_encoder]], of typeGPUCommandEncoder, readonly-
The
GPUCommandEncoderthat created this render pass encoder. [[attachment_size]], readonly-
Set to the following extents:
-
width, height= the dimensions of the pass’s render attachments
-
[[occlusion_query_set]], of typeGPUQuerySet, readonly-
The
GPUQuerySetto store occlusion query results for the pass, which is initialized withGPURenderPassDescriptor.occlusionQuerySetat pass creation time. [[endTimestampWrite]], of type GPU command?, readonly, defaulting tonull-
GPU command, if any, writing a timestamp when the pass ends.
[[maxDrawCount]]of typeGPUSize64, readonly-
The maximum number of draws allowed in this pass.
[[occlusion_query_active]], of typeboolean-
Whether the pass’s
[[occlusion_query_set]]is being written.
When executing encoded render pass commands as part of a GPUCommandBuffer,
an internal
RenderState object is used
to track the current state required for rendering.
RenderState has the following queue timeline properties:
[[occlusionQueryIndex]], of typeGPUSize32-
The index into
[[occlusion_query_set]]at which to store the occlusion query results. [[viewport]]-
Current viewport rectangle and depth range. Initially set to the following values:
-
x, y=0.0, 0.0 -
width, height= the dimensions of the pass’s render targets -
minDepth, maxDepth=0.0, 1.0
-
[[scissorRect]]-
Current scissor rectangle. Initially set to the following values:
-
x, y=0, 0 -
width, height= the dimensions of the pass’s render targets
-
[[blendConstant]], of typeGPUColor-
Current blend constant value, initially
[0, 0, 0, 0]. [[stencilReference]], of typeGPUStencilValue-
Current stencil reference value, initially
0. [[colorAttachments]], of type sequence<GPURenderPassColorAttachment?>-
The color attachments and state for this render pass.
[[depthStencilAttachment]], of typeGPURenderPassDepthStencilAttachment?-
The depth/stencil attachment and state for this render pass.
Render passes also have framebuffer memory, which contains the texel data associated with each attachment that is written into by draw commands and read from for blending and depth/stencil testing.
Note: Depending on the GPU hardware, framebuffer memory may be the memory allocated by the attachment textures or may be a separate area of memory that the texture data is copied to and from, such as with tile-based architectures.
17.1.1. Render Pass Encoder Creation
dictionary {GPURenderPassTimestampWrites required GPUQuerySet querySet ;GPUSize32 beginningOfPassWriteIndex ;GPUSize32 endOfPassWriteIndex ; };
querySet, of type GPUQuerySet-
The
GPUQuerySet, of type"timestamp", that the query results will be written to. beginningOfPassWriteIndex, of type GPUSize32-
If defined, indicates the query index in
querySetinto which the timestamp at the beginning of the render pass will be written. endOfPassWriteIndex, of type GPUSize32-
If defined, indicates the query index in
querySetinto which the timestamp at the end of the render pass will be written.
Note: Timestamp query values are written in nanoseconds, but how the value is determined is implementation-defined and may not increase monotonically. See § 20.4 Timestamp Query for details.
dictionary :GPURenderPassDescriptor GPUObjectDescriptorBase {required sequence <GPURenderPassColorAttachment ?>colorAttachments ;GPURenderPassDepthStencilAttachment depthStencilAttachment ;GPUQuerySet occlusionQuerySet ;GPURenderPassTimestampWrites timestampWrites ;GPUSize64 maxDrawCount = 50000000; };
colorAttachments, of typesequence<GPURenderPassColorAttachment?>-
The set of
GPURenderPassColorAttachmentvalues in this sequence defines which color attachments will be output to when executing this render pass.Due to usage compatibility, no color attachment may alias another attachment or any resource used inside the render pass.
depthStencilAttachment, of type GPURenderPassDepthStencilAttachment-
The
GPURenderPassDepthStencilAttachmentvalue that defines the depth/stencil attachment that will be output to and tested against when executing this render pass.Due to usage compatibility, no writable depth/stencil attachment may alias another attachment or any resource used inside the render pass.
occlusionQuerySet, of type GPUQuerySet-
The
GPUQuerySetvalue defines where the occlusion query results will be stored for this pass. timestampWrites, of type GPURenderPassTimestampWrites-
Defines which timestamp values will be written for this pass, and where to write them to.
maxDrawCount, of type GPUSize64, defaulting to50000000-
The maximum number of draw calls that will be done in the render pass. Used by some implementations to size work injected before the render pass. Keeping the default value is a good default, unless it is known that more draw calls will be done.
Given a GPUDevice
device and GPURenderPassDescriptor
this, the following validation rules apply:
-
this.
colorAttachments.size must be ≤ device.[[limits]].maxColorAttachments. -
For each non-
nullcolorAttachment in this.colorAttachments:-
colorAttachment.
viewmust be valid to use with device. -
If colorAttachment.
resolveTargetis provided:-
colorAttachment.
resolveTargetmust be valid to use with device.
-
-
colorAttachment must meet the GPURenderPassColorAttachment Valid Usage rules.
-
-
If this.
depthStencilAttachmentis provided:-
this.
depthStencilAttachment.viewmust be valid to use with device. -
this.
depthStencilAttachmentmust meet the GPURenderPassDepthStencilAttachment Valid Usage rules.
-
-
There must exist at least one attachment, either:
-
A non-
nullvalue in this.colorAttachments, or -
A this.
depthStencilAttachment.
-
-
Validating GPURenderPassDescriptor’s color attachment bytes per sample(device, this.
colorAttachments) succeeds. -
All
views in non-nullmembers of this.colorAttachments, and this.depthStencilAttachment.viewif present, must have equalsampleCounts. -
For each
viewin non-nullmembers of this.colorAttachmentsand this.depthStencilAttachment.view, if present, the[[renderExtent]]must match. -
If this.
occlusionQuerySetis provided:-
this.
occlusionQuerySetmust be valid to use with device. -
this.
occlusionQuerySet.typemust beocclusion.
-
-
If this.
timestampWritesis provided:-
Validate timestampWrites(device, this.
timestampWrites) must return true.
-
Arguments:
-
GPUDevicedevice -
sequence<
GPURenderPassColorAttachment?> colorAttachments
Device timeline steps:
-
Let formats be an empty list<
GPUTextureFormat?> -
For each colorAttachment in colorAttachments:
-
If colorAttachment is
undefined, continue. -
Append colorAttachment.
view.[[descriptor]].formatto formats.
-
-
Calculating color attachment bytes per sample(formats) must be ≤ device.
[[limits]].maxColorAttachmentBytesPerSample.
17.1.1.1. Color Attachments
dictionary {GPURenderPassColorAttachment required (GPUTexture or GPUTextureView )view ;GPUIntegerCoordinate depthSlice ; (GPUTexture or GPUTextureView )resolveTarget ;GPUColor clearValue ;required GPULoadOp loadOp ;required GPUStoreOp storeOp ; };
view, of type(GPUTexture or GPUTextureView)-
Describes the texture subresource that will be output to for this color attachment. The subresource is determined by calling get as texture view(
view). depthSlice, of type GPUIntegerCoordinate-
Indicates the depth slice index of
"3d"viewthat will be output to for this color attachment. resolveTarget, of type(GPUTexture or GPUTextureView)-
Describes the texture subresource that will receive the resolved output for this color attachment if
viewis multisampled. The subresource is determined by calling get as texture view(resolveTarget). clearValue, of type GPUColor-
Indicates the value to clear
viewto prior to executing the render pass. If not provided, defaults to{r: 0, g: 0, b: 0, a: 0}. Ignored ifloadOpis not"clear".The components of
clearValueare all double values. They are converted to a texel value of texture format matching the render attachment. If conversion fails, a validation error is generated. loadOp, of type GPULoadOp-
Indicates the load operation to perform on
viewprior to executing the render pass.Note: It is recommended to prefer clearing; see
"clear"for details. storeOp, of type GPUStoreOp-
The store operation to perform on
viewafter executing the render pass.
Given a GPURenderPassColorAttachment
this:
-
Let renderViewDescriptor be this.
view.[[descriptor]]. -
Let renderTexture be this.
view.[[texture]]. -
All of the requirements in the following steps must be met.
-
renderViewDescriptor.
formatmust be a color renderable format. -
this.
viewmust be a renderable texture view. -
If renderViewDescriptor.
dimensionis"3d":-
this.
depthSlicemust be provided and must be < the depthOrArrayLayers of the logical miplevel-specific texture extent of the renderTexture subresource at mipmap level renderViewDescriptor.baseMipLevel.
Otherwise:
-
this.
depthSlicemust not be provided.
-
-
-
Converting the IDL value this.
clearValueto a texel value of texture format renderViewDescriptor.formatmust not throw aTypeError.Note: An error is not thrown if the value is out-of-range for the format but in-range for the corresponding WGSL primitive type (
f32,i32, oru32).
-
-
If this.
resolveTargetis provided:-
Let resolveViewDescriptor be this.
resolveTarget.[[descriptor]]. -
Let resolveTexture be this.
resolveTarget.[[texture]]. -
renderTexture.
sampleCountmust be > 1. -
resolveTexture.
sampleCountmust be 1. -
this.
resolveTargetmust be a non-3d renderable texture view. -
this.
resolveTarget.[[renderExtent]]and this.view.[[renderExtent]]must match. -
resolveViewDescriptor.
formatmust equal renderViewDescriptor.format. -
resolveViewDescriptor.
formatmust support resolve according to § 26.1.1 Plain color formats.
-
-
GPUTextureView
view is a renderable texture view
if the all of the requirements in the following device timeline steps are met:
-
Let descriptor be view.
[[descriptor]]. -
descriptor.
usagemust containRENDER_ATTACHMENT. -
descriptor.
dimensionmust be"2d"or"2d-array"or"3d". -
descriptor.
mipLevelCountmust be 1. -
descriptor.
arrayLayerCountmust be 1. -
descriptor.
aspectmust refer to all aspects of view.[[texture]].
Arguments:
-
sequence<
GPUTextureFormat?> formats
Returns: GPUSize32
-
Let total be 0.
-
For each non-null format in formats
-
Assert: format is a color renderable format.
-
Let renderTargetPixelByteCost be the render target pixel byte cost of format.
-
Let renderTargetComponentAlignment be the render target component alignment of format.
-
Round total up to the smallest multiple of renderTargetComponentAlignment greater than or equal to total.
-
Add renderTargetPixelByteCost to total.
-
-
Return total.
17.1.1.2. Depth/Stencil Attachments
dictionary {GPURenderPassDepthStencilAttachment required (GPUTexture or GPUTextureView )view ;float depthClearValue ;GPULoadOp depthLoadOp ;GPUStoreOp depthStoreOp ;boolean depthReadOnly =false ;GPUStencilValue stencilClearValue = 0;GPULoadOp stencilLoadOp ;GPUStoreOp stencilStoreOp ;boolean stencilReadOnly =false ; };
view, of type(GPUTexture or GPUTextureView)-
Describes the texture subresource that will be output to and read from for this depth/stencil attachment. The subresource is determined by calling get as texture view(
view). depthClearValue, of type float-
Indicates the value to clear
view’s depth component to prior to executing the render pass. Ignored ifdepthLoadOpis not"clear". Must be between 0.0 and 1.0, inclusive. depthLoadOp, of type GPULoadOp-
Indicates the load operation to perform on
view’s depth component prior to executing the render pass.Note: It is recommended to prefer clearing; see
"clear"for details. depthStoreOp, of type GPUStoreOp-
The store operation to perform on
view’s depth component after executing the render pass. depthReadOnly, of type boolean, defaulting tofalse-
Indicates that the depth component of
viewis read only. stencilClearValue, of type GPUStencilValue, defaulting to0-
Indicates the value to clear
view’s stencil component to prior to executing the render pass. Ignored ifstencilLoadOpis not"clear".The value will be converted to the type of the stencil aspect of view by taking the same number of LSBs as the number of bits in the stencil aspect of one texel of view.
stencilLoadOp, of type GPULoadOp-
Indicates the load operation to perform on
view’s stencil component prior to executing the render pass.Note: It is recommended to prefer clearing; see
"clear"for details. stencilStoreOp, of type GPUStoreOp-
The store operation to perform on
view’s stencil component after executing the render pass. stencilReadOnly, of type boolean, defaulting tofalse-
Indicates that the stencil component of
viewis read only.
Given a GPURenderPassDepthStencilAttachment
this, the following validation
rules apply:
-
this.
viewmust have a depth-or-stencil format. -
this.
viewmust be a renderable texture view. -
Let format be this.
view.[[descriptor]].format. -
If this.
depthLoadOpis"clear", this.depthClearValuemust be provided and must be between 0.0 and 1.0, inclusive. -
If format has a depth aspect and this.
depthReadOnlyisfalse:-
this.
depthLoadOpmust be provided. -
this.
depthStoreOpmust be provided.
Otherwise:
-
this.
depthLoadOpmust not be provided. -
this.
depthStoreOpmust not be provided.
-
-
If format has a stencil aspect and this.
stencilReadOnlyisfalse:-
this.
stencilLoadOpmust be provided. -
this.
stencilStoreOpmust be provided.
Otherwise:
-
this.
stencilLoadOpmust not be provided. -
this.
stencilStoreOpmust not be provided.
-
17.1.1.3. Load & Store Operations
enum {GPULoadOp "load" ,"clear" , };
"load"-
Loads the existing value for this attachment into the render pass.
"clear"-
Loads a clear value for this attachment into the render pass.
Note: On some GPU hardware (primarily mobile),
"clear"is significantly cheaper because it avoids loading data from main memory into tile-local memory. On other GPU hardware, there isn’t a significant difference. As a result, it is recommended to use"clear"rather than"load"in cases where the initial value doesn’t matter (e.g. the render target will be cleared using a skybox).
enum {GPUStoreOp "store" ,"discard" , };
"store"-
Stores the resulting value of the render pass for this attachment.
"discard"-
Discards the resulting value of the render pass for this attachment.
Note: Discarded attachments behave as if they are cleared to zero, but implementations are not required to perform a clear at the end of the render pass. Implementations which do not explicitly clear discarded attachments at the end of a pass must lazily clear them prior to the reading the attachment contents, which occurs via sampling, copies, attaching to a later render pass with
"load", displaying or reading back the canvas (get a copy of the image contents of a context), etc.
17.1.1.4. Render Pass Layout
GPURenderPassLayout
declares the layout of the render targets of a GPURenderBundle.
It is also used internally to describe
GPURenderPassEncoder
layouts and
GPURenderPipeline
layouts.
It determines compatibility between render passes, render bundles, and render pipelines.
dictionary :GPURenderPassLayout GPUObjectDescriptorBase {required sequence <GPUTextureFormat ?>colorFormats ;GPUTextureFormat depthStencilFormat ;GPUSize32 sampleCount = 1; };
colorFormats, of typesequence<GPUTextureFormat?>-
A list of the
GPUTextureFormats of the color attachments for this pass or bundle. depthStencilFormat, of type GPUTextureFormat-
The
GPUTextureFormatof the depth/stencil attachment for this pass or bundle. sampleCount, of type GPUSize32, defaulting to1-
Number of samples per pixel in the attachments for this pass or bundle.
GPURenderPassLayout
values are equal if:
-
Their
depthStencilFormatandsampleCountare equal, and -
Their
colorFormatsare equal ignoring any trailingnulls.
Arguments:
-
GPURenderPassDescriptordescriptor
Returns: GPURenderPassLayout
Device timeline steps:
-
Let layout be a new
GPURenderPassLayoutobject. -
For each colorAttachment in descriptor.
colorAttachments:-
If colorAttachment is not
null:-
Set layout.
sampleCountto colorAttachment.view.[[texture]].sampleCount. -
Append colorAttachment.
view.[[descriptor]].formatto layout.colorFormats.
-
-
Otherwise:
-
Append
nullto layout.colorFormats.
-
-
-
Let depthStencilAttachment be descriptor.
depthStencilAttachment. -
If depthStencilAttachment is not
null:-
Let view be depthStencilAttachment.
view. -
Set layout.
sampleCountto view.[[texture]].sampleCount. -
Set layout.
depthStencilFormatto view.[[descriptor]].format.
-
-
Return layout.
Arguments:
-
GPURenderPipelineDescriptordescriptor
Returns: GPURenderPassLayout
Device timeline steps:
-
Let layout be a new
GPURenderPassLayoutobject. -
Set layout.
sampleCountto descriptor.multisample.count. -
If descriptor.
depthStencilis provided:-
Set layout.
depthStencilFormatto descriptor.depthStencil.format.
-
-
If descriptor.
fragmentis provided:-
For each colorTarget in descriptor.
fragment.targets:-
Append colorTarget.
formatto layout.colorFormatsif colorTarget is notnull, or appendnullotherwise.
-
-
-
Return layout.
17.1.2. Finalization
The render pass encoder can be ended by calling end()
once the user
has finished recording commands for the pass. Once end()
has been
called the render pass encoder can no longer be used.
end()-
Completes recording of the render pass commands sequence.
Called on:GPURenderPassEncoderthis.Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Let parentEncoder be this.
[[command_encoder]]. -
If any of the following requirements are unmet, generate a validation error and return.
-
If any of the following requirements are unmet, invalidate parentEncoder and return.
-
this must be valid.
-
this.
[[usage scope]]must satisfy usage scope validation. -
this.
[[debug_group_stack]]must be empty. -
this.
[[occlusion_query_active]]must befalse. -
this.
[[drawCount]]must be ≤ this.[[maxDrawCount]].
-
-
Extend parentEncoder.
[[commands]]with this.[[commands]]. -
If this.
[[endTimestampWrite]]is notnull:-
Extend parentEncoder.
[[commands]]with this.[[endTimestampWrite]].
-
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
For each non-
nullcolorAttachment in renderState.[[colorAttachments]]:-
Let colorView be colorAttachment.
view. -
If colorView.
[[descriptor]].dimensionis:"3d"-
Let colorSubregion be colorAttachment.
depthSliceof colorView. - Otherwise
-
Let colorSubregion be colorView.
-
If colorAttachment.
resolveTargetis notnull:-
Resolve the multiple samples of every texel of colorSubregion to a single sample and copy to colorAttachment.
resolveTarget.
-
-
If colorAttachment.
loadOpis:"store"-
Ensure the contents of the framebuffer memory associated with colorSubregion are stored in colorSubregion.
"discard"-
Set every texel of colorSubregion to zero.
-
-
Let depthStencilAttachment be renderState.
[[depthStencilAttachment]]. -
If depthStencilAttachment is not
null:-
If depthStencilAttachment.
depthLoadOpis:- Not provided
-
Assert that depthStencilAttachment.
depthReadOnlyistrueand leave the depth subresource of depthStencilView unchanged. "store"-
Ensure the contents of the framebuffer memory associated with the depth subresource of depthStencilView are stored in depthStencilView.
"discard"-
Set every texel in the depth subresource of depthStencilView to zero.
-
If depthStencilAttachment.
stencilLoadOpis:- Not provided
-
Assert that depthStencilAttachment.
stencilReadOnlyistrueand leave the stencil subresource of depthStencilView unchanged. "store"-
Ensure the contents of the framebuffer memory associated with the stencil subresource of depthStencilView are stored in depthStencilView.
"discard"-
Set every texel in the stencil subresource depthStencilView to zero.
-
-
Let renderState be
null.
Note: Discarded attachments behave as if they are cleared to zero, but implementations are not required to perform a clear at the end of the render pass. See the note on
"discard"for additional details.Note: Read-only depth-stencil attachments can be thought of as implicitly using the
"store"operation, but since their content is unchanged during the render pass implementations don’t need to update the attachment. Validation that requires the store op to not be provided for read-only attachments is done in GPURenderPassDepthStencilAttachment Valid Usage. -
17.2. GPURenderCommandsMixin
GPURenderCommandsMixin
defines rendering commands common to GPURenderPassEncoder
and GPURenderBundleEncoder.
interface mixin GPURenderCommandsMixin {undefined setPipeline (GPURenderPipeline pipeline );undefined setIndexBuffer (GPUBuffer buffer ,GPUIndexFormat indexFormat ,optional GPUSize64 offset = 0,optional GPUSize64 size );undefined setVertexBuffer (GPUIndex32 slot ,GPUBuffer ?buffer ,optional GPUSize64 offset = 0,optional GPUSize64 size );undefined draw (GPUSize32 vertexCount ,optional GPUSize32 instanceCount = 1,optional GPUSize32 firstVertex = 0,optional GPUSize32 firstInstance = 0);undefined drawIndexed (GPUSize32 indexCount ,optional GPUSize32 instanceCount = 1,optional GPUSize32 firstIndex = 0,optional GPUSignedOffset32 baseVertex = 0,optional GPUSize32 firstInstance = 0);undefined drawIndirect (GPUBuffer indirectBuffer ,GPUSize64 indirectOffset );undefined drawIndexedIndirect (GPUBuffer indirectBuffer ,GPUSize64 indirectOffset ); };
GPURenderCommandsMixin
assumes the presence of
GPUObjectBase,
GPUCommandsMixin,
and GPUBindingCommandsMixin
members on the same object.
It must only be included by interfaces which also include those mixins.
GPURenderCommandsMixin
has the following device timeline properties:
[[layout]], of typeGPURenderPassLayout, readonly-
The layout of the render pass.
[[depthReadOnly]], of typeboolean, readonly-
If
true, indicates that the depth component is not modified. [[stencilReadOnly]], of typeboolean, readonly-
If
true, indicates that the stencil component is not modified. [[usage scope]], of type usage scope, initially empty-
The usage scope for this render pass or bundle.
[[pipeline]], of typeGPURenderPipeline, initiallynull-
The current
GPURenderPipeline. [[index_buffer]], of typeGPUBuffer, initiallynull-
The current buffer to read index data from.
[[index_format]], of typeGPUIndexFormat-
The format of the index data in
[[index_buffer]]. [[index_buffer_offset]], of typeGPUSize64-
The offset in bytes of the section of
[[index_buffer]]currently set. [[index_buffer_size]], of typeGPUSize64-
The size in bytes of the section of
[[index_buffer]]currently set, initially0. [[vertex_buffers]], of type ordered map<slot,GPUBuffer>, initially empty-
The current
GPUBuffers to read vertex data from for each slot. [[vertex_buffer_sizes]], of type ordered map<slot,GPUSize64>, initially empty-
The size in bytes of the section of
GPUBuffercurrently set for each slot. [[drawCount]], of typeGPUSize64-
The number of draw commands recorded in this encoder.
GPURenderCommandsMixin
encoder which
issues the steps of a GPU Command
command with RenderState renderState, run the
following device
timeline steps:
-
Append command to encoder.
[[commands]]. -
When command is executed as part of a
GPUCommandBuffercommandBuffer:-
Issue the steps of command with commandBuffer.
[[renderState]]as renderState.
-
17.2.1. Drawing
setPipeline(pipeline)-
Sets the current
GPURenderPipeline.Called on:GPURenderCommandsMixinthis.Arguments:
Arguments for the GPURenderCommandsMixin.setPipeline(pipeline) method. Parameter Type Nullable Optional Description pipelineGPURenderPipeline✘ ✘ The render pipeline to use for subsequent drawing commands. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Let pipelineTargetsLayout be derive render targets layout from pipeline(pipeline.
[[descriptor]]). -
If any of the following conditions are unsatisfied, invalidate this and return.
-
pipeline is valid to use with this.
-
this.
[[layout]]equals pipelineTargetsLayout. -
If pipeline.
[[writesDepth]]: this.[[depthReadOnly]]must befalse. -
If pipeline.
[[writesStencil]]: this.[[stencilReadOnly]]must befalse.
-
-
Set this.
[[pipeline]]to be pipeline.
-
setIndexBuffer(buffer, indexFormat, offset, size)-
Sets the current index buffer.
Called on:GPURenderCommandsMixinthis.Arguments:
Arguments for the GPURenderCommandsMixin.setIndexBuffer(buffer, indexFormat, offset, size) method. Parameter Type Nullable Optional Description bufferGPUBuffer✘ ✘ Buffer containing index data to use for subsequent drawing commands. indexFormatGPUIndexFormat✘ ✘ Format of the index data contained in buffer. offsetGPUSize64✘ ✔ Offset in bytes into buffer where the index data begins. Defaults to 0.sizeGPUSize64✘ ✔ Size in bytes of the index data in buffer. Defaults to the size of the buffer minus the offset. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If size is missing, set size to max(0, buffer.
size- offset). -
If any of the following conditions are unsatisfied, invalidate this and return.
-
buffer is valid to use with this.
-
offset is a multiple of indexFormat’s byte size.
-
offset + size ≤ buffer.
size.
-
-
Add buffer to
[[usage scope]]with usage input. -
Set this.
[[index_buffer]]to be buffer. -
Set this.
[[index_format]]to be indexFormat. -
Set this.
[[index_buffer_offset]]to be offset. -
Set this.
[[index_buffer_size]]to be size.
-
setVertexBuffer(slot, buffer, offset, size)-
Sets the current vertex buffer for the given slot.
Called on:GPURenderCommandsMixinthis.Arguments:
Arguments for the GPURenderCommandsMixin.setVertexBuffer(slot, buffer, offset, size) method. Parameter Type Nullable Optional Description slotGPUIndex32✘ ✘ The vertex buffer slot to set the vertex buffer for. bufferGPUBuffer?✔ ✘ Buffer containing vertex data to use for subsequent drawing commands. offsetGPUSize64✘ ✔ Offset in bytes into buffer where the vertex data begins. Defaults to 0.sizeGPUSize64✘ ✔ Size in bytes of the vertex data in buffer. Defaults to the size of the buffer minus the offset. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Let bufferSize be 0 if buffer is
null, or buffer.sizeif not. -
If size is missing, set size to max(0, bufferSize - offset).
-
If any of the following requirements are unmet, invalidate this and return.
-
slot must be < this.
[[device]].[[limits]].maxVertexBuffers. -
offset must be a multiple of 4.
-
offset + size must be ≤ bufferSize.
-
-
If buffer is
null:-
Remove this.
[[vertex_buffers]][slot]. -
Remove this.
[[vertex_buffer_sizes]][slot].
Otherwise:
-
If any of the following requirements are unmet, invalidate this and return.
-
buffer must be valid to use with this.
-
-
Add buffer to
[[usage scope]]with usage input. -
Set this.
[[vertex_buffers]][slot] to be buffer. -
Set this.
[[vertex_buffer_sizes]][slot] to be size.
-
-
draw(vertexCount, instanceCount, firstVertex, firstInstance)-
Draws primitives. See § 23.2 Rendering for the detailed specification.
Called on:GPURenderCommandsMixinthis.Arguments:
Arguments for the GPURenderCommandsMixin.draw(vertexCount, instanceCount, firstVertex, firstInstance) method. Parameter Type Nullable Optional Description vertexCountGPUSize32✘ ✘ The number of vertices to draw. instanceCountGPUSize32✘ ✔ The number of instances to draw. firstVertexGPUSize32✘ ✔ Offset into the vertex buffers, in vertices, to begin drawing from. firstInstanceGPUSize32✘ ✔ First instance to draw. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
All of the requirements in the following steps must be met. If any are unmet, invalidate this and return.
-
It must be valid to draw with this.
-
Let buffers be this.
[[pipeline]].[[descriptor]].vertex.buffers. -
For each
GPUIndex32slot from0to buffers.size (non-inclusive):-
If buffers[slot] is
null, continue. -
Let bufferSize be this.
[[vertex_buffer_sizes]][slot]. -
Let stride be buffers[slot].
arrayStride. -
Let attributes be buffers[slot].
attributes -
Let lastStride be the maximum value of (attribute.
offset+ byteSize(attribute.format)) over each attribute in attributes, or 0 if attributes is empty. -
Let strideCount be computed based on buffers[slot].
stepMode:"vertex"-
firstVertex + vertexCount
"instance"-
firstInstance + instanceCount
-
If strideCount ≠
0:-
(strideCount −
1) × stride + lastStride must be ≤ bufferSize.
-
-
-
-
Increment this.
[[drawCount]]by 1. -
Let bindingState be a snapshot of this’s current state.
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Draw instanceCount instances, starting with instance firstInstance, of primitives consisting of vertexCount vertices, starting with vertex firstVertex, with the states from bindingState and renderState.
-
drawIndexed(indexCount, instanceCount, firstIndex, baseVertex, firstInstance)-
Draws indexed primitives. See § 23.2 Rendering for the detailed specification.
Called on:GPURenderCommandsMixinthis.Arguments:
Arguments for the GPURenderCommandsMixin.drawIndexed(indexCount, instanceCount, firstIndex, baseVertex, firstInstance) method. Parameter Type Nullable Optional Description indexCountGPUSize32✘ ✘ The number of indices to draw. instanceCountGPUSize32✘ ✔ The number of instances to draw. firstIndexGPUSize32✘ ✔ Offset into the index buffer, in indices, begin drawing from. baseVertexGPUSignedOffset32✘ ✔ Added to each index value before indexing into the vertex buffers. firstInstanceGPUSize32✘ ✔ First instance to draw. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
It is valid to draw indexed with this.
-
firstIndex + indexCount ≤ this.
[[index_buffer_size]]÷ this.[[index_format]]’s byte size; -
Let buffers be this.
[[pipeline]].[[descriptor]].vertex.buffers. -
For each
GPUIndex32slot from0to buffers.size (non-inclusive):-
If buffers[slot] is
null, continue. -
Let bufferSize be this.
[[vertex_buffer_sizes]][slot]. -
Let stride be buffers[slot].
arrayStride. -
Let lastStride be max(attribute.
offset+ byteSize(attribute.format)) for each attribute in buffers[slot].attributes. -
Let strideCount be firstInstance + instanceCount.
-
If buffers[slot].
stepModeis"instance"and strideCount ≠0:-
Ensure (strideCount −
1) × stride + lastStride ≤ bufferSize.
-
-
-
-
Increment this.
[[drawCount]]by 1. -
Let bindingState be a snapshot of this’s current state.
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Draw instanceCount instances, starting with instance firstInstance, of primitives consisting of indexCount indexed vertices, starting with index firstIndex from vertex baseVertex, with the states from bindingState and renderState.
Note: WebGPU applications should never use index data with indices out of bounds of any bound vertex buffer that has
GPUVertexStepMode"vertex". WebGPU implementations have different ways of handling this, and therefore a range of behaviors is allowed. Either the whole draw call is discarded, or the access to those attributes out of bounds is described by WGSL’s invalid memory reference. -
drawIndirect(indirectBuffer, indirectOffset)-
Draws primitives using parameters read from a
GPUBuffer. See § 23.2 Rendering for the detailed specification.The indirect draw parameters encoded in the buffer must be a tightly packed block of four 32-bit unsigned integer values (16 bytes total), given in the same order as the arguments for
draw(). For example:let drawIndirectParameters= new Uint32Array( 4 ); drawIndirectParameters[ 0 ] = vertexCount; drawIndirectParameters[ 1 ] = instanceCount; drawIndirectParameters[ 2 ] = firstVertex; drawIndirectParameters[ 3 ] = firstInstance; The value corresponding to
firstInstancemust be 0, unless the"indirect-first-instance"feature is enabled. If the"indirect-first-instance"feature is not enabled andfirstInstanceis not zero thedrawIndirect()call will be treated as a no-op.Called on:GPURenderCommandsMixinthis.Arguments:
Arguments for the GPURenderCommandsMixin.drawIndirect(indirectBuffer, indirectOffset) method. Parameter Type Nullable Optional Description indirectBufferGPUBuffer✘ ✘ Buffer containing the indirect draw parameters. indirectOffsetGPUSize64✘ ✘ Offset in bytes into indirectBuffer where the drawing data begins. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
It is valid to draw with this.
-
indirectBuffer is valid to use with this.
-
indirectOffset + sizeof(indirect draw parameters) ≤ indirectBuffer.
size. -
indirectOffset is a multiple of 4.
-
-
Add indirectBuffer to
[[usage scope]]with usage input. -
Increment this.
[[drawCount]]by 1. -
Let bindingState be a snapshot of this’s current state.
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Let vertexCount be an unsigned 32-bit integer read from indirectBuffer at indirectOffset bytes.
-
Let instanceCount be an unsigned 32-bit integer read from indirectBuffer at (indirectOffset + 4) bytes.
-
Let firstVertex be an unsigned 32-bit integer read from indirectBuffer at (indirectOffset + 8) bytes.
-
Let firstInstance be an unsigned 32-bit integer read from indirectBuffer at (indirectOffset + 12) bytes.
-
Draw instanceCount instances, starting with instance firstInstance, of primitives consisting of vertexCount vertices, starting with vertex firstVertex, with the states from bindingState and renderState.
-
drawIndexedIndirect(indirectBuffer, indirectOffset)-
Draws indexed primitives using parameters read from a
GPUBuffer. See § 23.2 Rendering for the detailed specification.The indirect drawIndexed parameters encoded in the buffer must be a tightly packed block of five 32-bit values (20 bytes total), given in the same order as the arguments for
drawIndexed(). The value corresponding tobaseVertexis a signed 32-bit integer, and all others are unsigned 32-bit integers. For example:let drawIndexedIndirectParameters= new Uint32Array( 5 ); let drawIndexedIndirectParametersSigned= new Int32Array( drawIndexedIndirectParameters. buffer); drawIndexedIndirectParameters[ 0 ] = indexCount; drawIndexedIndirectParameters[ 1 ] = instanceCount; drawIndexedIndirectParameters[ 2 ] = firstIndex; // baseVertex is a signed value. drawIndexedIndirectParametersSigned[ 3 ] = baseVertex; drawIndexedIndirectParameters[ 4 ] = firstInstance; The value corresponding to
firstInstancemust be 0, unless the"indirect-first-instance"feature is enabled. If the"indirect-first-instance"feature is not enabled andfirstInstanceis not zero thedrawIndexedIndirect()call will be treated as a no-op.Called on:GPURenderCommandsMixinthis.Arguments:
Arguments for the GPURenderCommandsMixin.drawIndexedIndirect(indirectBuffer, indirectOffset) method. Parameter Type Nullable Optional Description indirectBufferGPUBuffer✘ ✘ Buffer containing the indirect drawIndexed parameters. indirectOffsetGPUSize64✘ ✘ Offset in bytes into indirectBuffer where the drawing data begins. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
It is valid to draw indexed with this.
-
indirectBuffer is valid to use with this.
-
indirectOffset + sizeof(indirect drawIndexed parameters) ≤ indirectBuffer.
size. -
indirectOffset is a multiple of 4.
-
-
Add indirectBuffer to
[[usage scope]]with usage input. -
Increment this.
[[drawCount]]by 1. -
Let bindingState be a snapshot of this’s current state.
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Let indexCount be an unsigned 32-bit integer read from indirectBuffer at indirectOffset bytes.
-
Let instanceCount be an unsigned 32-bit integer read from indirectBuffer at (indirectOffset + 4) bytes.
-
Let firstIndex be an unsigned 32-bit integer read from indirectBuffer at (indirectOffset + 8) bytes.
-
Let baseVertex be a signed 32-bit integer read from indirectBuffer at (indirectOffset + 12) bytes.
-
Let firstInstance be an unsigned 32-bit integer read from indirectBuffer at (indirectOffset + 16) bytes.
-
Draw instanceCount instances, starting with instance firstInstance, of primitives consisting of indexCount indexed vertices, starting with index firstIndex from vertex baseVertex, with the states from bindingState and renderState.
-
GPURenderCommandsMixin
encoder,
run the following device
timeline steps:
-
If any of the following conditions are unsatisfied, return
false:-
Validate encoder bind groups(encoder, encoder.
[[pipeline]]) must betrue. -
Let pipelineDescriptor be encoder.
[[pipeline]].[[descriptor]]. -
For each
GPUIndex32slot0to pipelineDescriptor.vertex.buffers.size:-
If pipelineDescriptor.
vertex.buffers[slot] is notnull, encoder.[[vertex_buffers]]must contain slot.
-
-
Validate
maxBindGroupsPlusVertexBuffers:-
Let bindGroupSpaceUsed be (the maximum key in encoder.
[[bind_groups]]) + 1. -
Let vertexBufferSpaceUsed be (the maximum key in encoder.
[[vertex_buffers]]) + 1. -
bindGroupSpaceUsed + vertexBufferSpaceUsed must be ≤ encoder.
[[device]].[[limits]].maxBindGroupsPlusVertexBuffers.
-
-
-
Otherwise return
true.
GPURenderCommandsMixin
encoder,
run the following device
timeline steps:
-
If any of the following conditions are unsatisfied, return
false:-
It must be valid to draw with encoder.
-
encoder.
[[index_buffer]]must not benull. -
Let topology be encoder.
[[pipeline]].[[descriptor]].primitive.topology. -
If topology is
"line-strip"or"triangle-strip":-
encoder.
[[index_format]]must equal encoder.[[pipeline]].[[descriptor]].primitive.stripIndexFormat.
-
-
-
Otherwise return
true.
17.2.2. Rasterization state
The GPURenderPassEncoder
has several methods which affect how draw commands are rasterized to
attachments used by this encoder.
setViewport(x, y, width, height, minDepth, maxDepth)-
Sets the viewport used during the rasterization stage to linearly map from normalized device coordinates to viewport coordinates.
Called on:GPURenderPassEncoderthis.Arguments:
Arguments for the GPURenderPassEncoder.setViewport(x, y, width, height, minDepth, maxDepth) method. Parameter Type Nullable Optional Description xfloat✘ ✘ Minimum X value of the viewport in pixels. yfloat✘ ✘ Minimum Y value of the viewport in pixels. widthfloat✘ ✘ Width of the viewport in pixels. heightfloat✘ ✘ Height of the viewport in pixels. minDepthfloat✘ ✘ Minimum depth value of the viewport. maxDepthfloat✘ ✘ Maximum depth value of the viewport. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Let maxViewportRange be this.
limits.maxTextureDimension2D×2. -
If any of the following conditions are unsatisfied, invalidate this and return.
-
x ≥ -maxViewportRange
-
y ≥ -maxViewportRange
-
0≤ width ≤ this.limits.maxTextureDimension2D -
0≤ height ≤ this.limits.maxTextureDimension2D -
x + width ≤ maxViewportRange −
1 -
y + height ≤ maxViewportRange −
1 -
0.0≤ minDepth ≤1.0 -
0.0≤ maxDepth ≤1.0 -
minDepth ≤ maxDepth
-
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Round x, y, width, and height to some uniform precision, no less precise than integer rounding.
-
Set renderState.
[[viewport]]to the extents x, y, width, height, minDepth, and maxDepth.
-
setScissorRect(x, y, width, height)-
Sets the scissor rectangle used during the rasterization stage. After transformation into viewport coordinates any fragments which fall outside the scissor rectangle will be discarded.
Called on:GPURenderPassEncoderthis.Arguments:
Arguments for the GPURenderPassEncoder.setScissorRect(x, y, width, height) method. Parameter Type Nullable Optional Description xGPUIntegerCoordinate✘ ✘ Minimum X value of the scissor rectangle in pixels. yGPUIntegerCoordinate✘ ✘ Minimum Y value of the scissor rectangle in pixels. widthGPUIntegerCoordinate✘ ✘ Width of the scissor rectangle in pixels. heightGPUIntegerCoordinate✘ ✘ Height of the scissor rectangle in pixels. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
x+width ≤ this.
[[attachment_size]].width. -
y+height ≤ this.
[[attachment_size]].height.
-
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Set renderState.
[[scissorRect]]to the extents x, y, width, and height.
-
setBlendConstant(color)-
Sets the constant blend color and alpha values used with
"constant"and"one-minus-constant"GPUBlendFactors.Called on:GPURenderPassEncoderthis.Arguments:
Arguments for the GPURenderPassEncoder.setBlendConstant(color) method. Parameter Type Nullable Optional Description colorGPUColor✘ ✘ The color to use when blending. Returns:
undefinedContent timeline steps:
-
? validate GPUColor shape(color).
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Set renderState.
[[blendConstant]]to color.
-
setStencilReference(reference)-
Sets the
[[stencilReference]]value used during stencil tests with the"replace"GPUStencilOperation.Called on:GPURenderPassEncoderthis.Arguments:
Arguments for the GPURenderPassEncoder.setStencilReference(reference) method. Parameter Type Nullable Optional Description referenceGPUStencilValue✘ ✘ The new stencil reference value. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Set renderState.
[[stencilReference]]to reference.
-
17.2.3. Queries
beginOcclusionQuery(queryIndex)-
Called on:
GPURenderPassEncoderthis.Arguments:
Arguments for the GPURenderPassEncoder.beginOcclusionQuery(queryIndex) method. Parameter Type Nullable Optional Description queryIndexGPUSize32✘ ✘ The index of the query in the query set. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
this.
[[occlusion_query_set]]is notnull. -
queryIndex < this.
[[occlusion_query_set]].count. -
The query at same queryIndex must not have been previously written to in this pass.
-
this.
[[occlusion_query_active]]isfalse.
-
-
Set this.
[[occlusion_query_active]]totrue. -
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Set renderState.
[[occlusionQueryIndex]]to queryIndex.
-
endOcclusionQuery()-
Called on:
GPURenderPassEncoderthis.Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
this.
[[occlusion_query_active]]istrue.
-
-
Set this.
[[occlusion_query_active]]tofalse. -
Enqueue a render command on this which issues the subsequent steps on the Queue timeline with renderState when executed.
Queue timeline steps:-
Let passingFragments be non-zero if any fragment samples passed all per-fragment tests since the corresponding
beginOcclusionQuery()command was executed, and zero otherwise.Note: If no draw calls occurred, passingFragments is zero.
-
Write passingFragments into this.
[[occlusion_query_set]]at index renderState.[[occlusionQueryIndex]].
-
17.2.4. Bundles
executeBundles(bundles)-
Executes the commands previously recorded into the given
GPURenderBundles as part of this render pass.When a
GPURenderBundleis executed, it does not inherit the render pass’s pipeline, bind groups, or vertex and index buffers. After aGPURenderBundlehas executed, the render pass’s pipeline, bind group, and vertex/index buffer state is cleared (to the initial, empty values).Note: The state is cleared, not restored to the previous state. This occurs even if zero
GPURenderBundlesare executed.Called on:GPURenderPassEncoderthis.Arguments:
Arguments for the GPURenderPassEncoder.executeBundles(bundles) method. Parameter Type Nullable Optional Description bundlessequence<GPURenderBundle>✘ ✘ List of render bundles to execute. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
[[device]].
Device timeline steps:-
Validate the encoder state of this. If it returns false, return.
-
If any of the following conditions are unsatisfied, invalidate this and return.
-
For each bundle in bundles:
-
bundle must be valid to use with this.
-
this.
[[layout]]must equal bundle.[[layout]]. -
If this.
[[depthReadOnly]]is true, bundle.[[depthReadOnly]]must be true. -
If this.
[[stencilReadOnly]]is true, bundle.[[stencilReadOnly]]must be true.
-
-
-
For each bundle in bundles:
-
Increment this.
[[drawCount]]by bundle.[[drawCount]]. -
Merge bundle.
[[usage scope]]into this.[[usage scope]]. -
Enqueue a render command on this which issues the following steps on the Queue timeline with renderState when executed:
Queue timeline steps:-
Execute each command in bundle.
[[command_list]]with renderState.Note: renderState cannot be changed by executing render bundles. Binding state was already captured at bundle encoding time, and so isn’t used when executing bundles.
-
-
-
Reset the render pass binding state of this.
-
GPURenderPassEncoder
encoder run
the following device
timeline steps:
-
Clear encoder.
[[bind_groups]]. -
Set encoder.
[[pipeline]]tonull. -
Set encoder.
[[index_buffer]]tonull. -
Clear encoder.
[[vertex_buffers]].
18. Bundles
A bundle is a partial, limited pass that is encoded once and can then be executed multiple times as part of future pass encoders without expiring after use like typical command buffers. This can reduce the overhead of encoding and submission of commands which are issued repeatedly without changing.
18.1. GPURenderBundle
[Exposed =(Window ,Worker ),SecureContext ]interface GPURenderBundle { };GPURenderBundle includes GPUObjectBase ;
[[command_list]], of type list<GPU command>-
A list of GPU commands to be submitted to the
GPURenderPassEncoderwhen theGPURenderBundleis executed. [[usage scope]], of type usage scope, initially empty-
The usage scope for this render bundle, stored for later merging into the
GPURenderPassEncoder’s[[usage scope]]inexecuteBundles(). [[layout]], of typeGPURenderPassLayout-
The layout of the render bundle.
[[depthReadOnly]], of typeboolean-
If
true, indicates that the depth component is not modified by executing this render bundle. [[stencilReadOnly]], of typeboolean-
If
true, indicates that the stencil component is not modified by executing this render bundle. [[drawCount]], of typeGPUSize64-
The number of draw commands in this
GPURenderBundle.
18.1.1. Render Bundle Creation
dictionary :GPURenderBundleDescriptor GPUObjectDescriptorBase { };
[Exposed =(Window ,Worker ),SecureContext ]interface {GPURenderBundleEncoder GPURenderBundle finish (optional GPURenderBundleDescriptor descriptor = {}); };GPURenderBundleEncoder includes GPUObjectBase ;GPURenderBundleEncoder includes GPUCommandsMixin ;GPURenderBundleEncoder includes GPUDebugCommandsMixin ;GPURenderBundleEncoder includes GPUBindingCommandsMixin ;GPURenderBundleEncoder includes GPURenderCommandsMixin ;
createRenderBundleEncoder(descriptor)-
Creates a
GPURenderBundleEncoder.Called on:GPUDevicethis.Arguments:
Arguments for the GPUDevice.createRenderBundleEncoder(descriptor) method. Parameter Type Nullable Optional Description descriptorGPURenderBundleEncoderDescriptor✘ ✘ Description of the GPURenderBundleEncoderto create.Returns:
GPURenderBundleEncoderContent timeline steps:
-
? Validate texture format required features of each non-
nullelement of descriptor.colorFormatswith this.[[device]]. -
If descriptor.
depthStencilFormatis provided:-
? Validate texture format required features of descriptor.
depthStencilFormatwith this.[[device]].
-
-
Let e be ! create a new WebGPU object(this,
GPURenderBundleEncoder, descriptor). -
Issue the initialization steps on the Device timeline of this.
-
Return e.
Device timeline initialization steps:-
If any of the following conditions are unsatisfied generate a validation error, invalidate e and return.
-
this must not be lost.
-
descriptor.
colorFormats.size must be ≤ this.[[limits]].maxColorAttachments. -
For each non-
nullcolorFormat in descriptor.colorFormats:-
colorFormat must be a color renderable format.
-
-
Calculating color attachment bytes per sample(descriptor.
colorFormats) must be ≤ this.[[limits]].maxColorAttachmentBytesPerSample. -
If descriptor.
depthStencilFormatis provided:-
descriptor.
depthStencilFormatmust be a depth-or-stencil format.
-
-
There must exist at least one attachment, either:
-
A non-
nullvalue in descriptor.colorFormats, or -
A descriptor.
depthStencilFormat.
-
-
-
Set e.
[[layout]]to a copy of descriptor’s includedGPURenderPassLayoutinterface. -
Set e.
[[depthReadOnly]]to descriptor.depthReadOnly. -
Set e.
[[stencilReadOnly]]to descriptor.stencilReadOnly. -
Set e.
[[drawCount]]to 0.
-
18.1.2. Encoding
dictionary :GPURenderBundleEncoderDescriptor GPURenderPassLayout {boolean depthReadOnly =false ;boolean stencilReadOnly =false ; };
depthReadOnly, of type boolean, defaulting tofalse-
If
true, indicates that the render bundle does not modify the depth component of theGPURenderPassDepthStencilAttachmentof any render pass the render bundle is executed in. stencilReadOnly, of type boolean, defaulting tofalse-
If
true, indicates that the render bundle does not modify the stencil component of theGPURenderPassDepthStencilAttachmentof any render pass the render bundle is executed in.
18.1.3. Finalization
finish(descriptor)-
Completes recording of the render bundle commands sequence.
Called on:GPURenderBundleEncoderthis.Arguments:
Arguments for the GPURenderBundleEncoder.finish(descriptor) method. Parameter Type Nullable Optional Description descriptorGPURenderBundleDescriptor✘ ✔ Returns:
GPURenderBundleContent timeline steps:
-
Let renderBundle be a new
GPURenderBundle. -
Issue the finish steps on the Device timeline of this.
[[device]]. -
Return renderBundle.
Device timeline finish steps:-
Let validationSucceeded be
trueif all of the following requirements are met, andfalseotherwise.-
this must be valid.
-
this.
[[usage scope]]must satisfy usage scope validation. -
this.
[[debug_group_stack]]must be empty.
-
-
If validationSucceeded is
false, then:-
Return an invalidated
GPURenderBundle.
-
Set renderBundle.
[[command_list]]to this.[[commands]]. -
Set renderBundle.
[[usage scope]]to this.[[usage scope]]. -
Set renderBundle.
[[drawCount]]to this.[[drawCount]].
-
19. Queues
19.1. GPUQueueDescriptor
GPUQueueDescriptor
describes a queue request.
dictionary GPUQueueDescriptor :GPUObjectDescriptorBase { };
19.2. GPUQueue
[Exposed =(Window ,Worker ),SecureContext ]interface GPUQueue {undefined submit (sequence <GPUCommandBuffer >commandBuffers );Promise <undefined >onSubmittedWorkDone ();undefined writeBuffer (GPUBuffer buffer ,GPUSize64 bufferOffset ,AllowSharedBufferSource data ,optional GPUSize64 dataOffset = 0,optional GPUSize64 size );undefined writeTexture (GPUTexelCopyTextureInfo destination ,AllowSharedBufferSource data ,GPUTexelCopyBufferLayout dataLayout ,GPUExtent3D size );undefined copyExternalImageToTexture (GPUCopyExternalImageSourceInfo source ,GPUCopyExternalImageDestInfo destination ,GPUExtent3D copySize ); };GPUQueue includes GPUObjectBase ;
GPUQueue has
the following methods:
writeBuffer(buffer, bufferOffset, data, dataOffset, size)-
Issues a write operation of the provided data into a
GPUBuffer.Called on:GPUQueuethis.Arguments:
Arguments for the GPUQueue.writeBuffer(buffer, bufferOffset, data, dataOffset, size) method. Parameter Type Nullable Optional Description bufferGPUBuffer✘ ✘ The buffer to write to. bufferOffsetGPUSize64✘ ✘ Offset in bytes into buffer to begin writing at. dataAllowSharedBufferSource✘ ✘ Data to write into buffer. dataOffsetGPUSize64✘ ✔ Offset in into data to begin writing from. Given in elements if data is a TypedArrayand bytes otherwise.sizeGPUSize64✘ ✔ Size of content to write from data to buffer. Given in elements if data is a TypedArrayand bytes otherwise.Returns:
undefinedContent timeline steps:
-
If data is an
ArrayBufferorDataView, let the element type be "byte". Otherwise, data is a TypedArray; let the element type be the type of the TypedArray. -
Let dataSize be the size of data, in elements.
-
If size is missing, let contentsSize be dataSize − dataOffset. Otherwise, let contentsSize be size.
-
If any of the following conditions are unsatisfied, throw an
OperationErrorand return.-
contentsSize ≥ 0.
-
dataOffset + contentsSize ≤ dataSize.
-
contentsSize, converted to bytes, is a multiple of 4 bytes.
-
-
Let dataContents be a copy of the bytes held by the buffer source data.
-
Let contents be the contentsSize elements of dataContents starting at an offset of dataOffset elements.
-
Issue the subsequent steps on the Device timeline of this.
Device timeline steps:-
If any of the following conditions are unsatisfied, generate a validation error and return.
-
buffer is valid to use with this.
-
buffer.
[[internal state]]is "available". -
bufferOffset, converted to bytes, is a multiple of 4 bytes.
-
bufferOffset + contentsSize, converted to bytes, ≤ buffer.
sizebytes.
-
-
Issue the subsequent steps on the Queue timeline of this.
Queue timeline steps:-
Write contents into buffer starting at bufferOffset.
-
writeTexture(destination, data, dataLayout, size)-
Issues a write operation of the provided data into a
GPUTexture.Called on:GPUQueuethis.Arguments:
Arguments for the GPUQueue.writeTexture(destination, data, dataLayout, size) method. Parameter Type Nullable Optional Description destinationGPUTexelCopyTextureInfo✘ ✘ The texture subresource and origin to write to. dataAllowSharedBufferSource✘ ✘ Data to write into destination. dataLayoutGPUTexelCopyBufferLayout✘ ✘ Layout of the content in data. sizeGPUExtent3D✘ ✘ Extents of the content to write from data to destination. Returns:
undefinedContent timeline steps:
-
? validate GPUOrigin3D shape(destination.
origin). -
? validate GPUExtent3D shape(size).
-
Let dataBytes be a copy of the bytes held by the buffer source data.
Note: This is described as copying all of data to the device timeline, but in practice data could be much larger than necessary. Implementations should optimize by copying only the necessary bytes.
-
Issue the subsequent steps on the Device timeline of this.
Device timeline steps:-
Let aligned be
false. -
Let dataLength be dataBytes.length.
-
If any of the following conditions are unsatisfied, generate a validation error and return.
-
destination.
texture.[[destroyed]]isfalse. -
validating texture buffer copy(destination, dataLayout, dataLength, size,
COPY_DST, aligned) returnstrue.
Note: unlike
GPUCommandEncoder.copyBufferToTexture(), there is no alignment requirement on either dataLayout.bytesPerRowor dataLayout.offset. -
-
Issue the subsequent steps on the Queue timeline of this.
Queue timeline steps:-
Let blockWidth be the texel block width of destination.
texture. -
Let blockHeight be the texel block height of destination.
texture. -
Let dstOrigin be destination.
origin; -
Let dstBlockOriginX be (dstOrigin.x ÷ blockWidth).
-
Let dstBlockOriginY be (dstOrigin.y ÷ blockHeight).
-
Let blockColumns be (copySize.width ÷ blockWidth).
-
Let blockRows be (copySize.height ÷ blockHeight).
-
Assert that dstBlockOriginX, dstBlockOriginY, blockColumns, and blockRows are integers.
-
For each z in the range [0, copySize.depthOrArrayLayers − 1]:
-
Let dstSubregion be texture copy sub-region (z + dstOrigin.z) of destination.
-
For each y in the range [0, blockRows − 1]:
-
For each x in the range [0, blockColumns − 1]:
-
Let blockOffset be the texel block byte offset of dataLayout for (x, y, z) of destination.
texture. -
Set texel block (dstBlockOriginX + x, dstBlockOriginY + y) of dstSubregion to be an equivalent texel representation to the texel block described by dataBytes at offset blockOffset.
-
-
-
-
copyExternalImageToTexture(source, destination, copySize)-
Issues a copy operation of the contents of a platform image/canvas into the destination texture.
This operation performs color encoding into the destination encoding according to the parameters of
GPUCopyExternalImageDestInfo.Copying into a
-srgbtexture results in the same texture bytes, not the same decoded values, as copying into the corresponding non--srgbformat. Thus, after a copy operation, sampling the destination texture has different results depending on whether its format is-srgb, all else unchanged.NOTE:When copying from a"webgl"/"webgl2"context canvas, the WebGL Drawing Buffer may be not exist during certain points in the frame presentation cycle (after the image has been moved to the compositor for display). To avoid this, either:-
Issue
copyExternalImageToTexture()in the same task with WebGL rendering operation, to ensure the copy occurs before the WebGL canvas is presented. -
If not possible, set the
preserveDrawingBufferoption inWebGLContextAttributestotrue, so that the drawing buffer will still contain a copy of the frame contents after they’ve been presented. Note, this extra copy may have a performance cost.
Called on:GPUQueuethis.Arguments:
Arguments for the GPUQueue.copyExternalImageToTexture(source, destination, copySize) method. Parameter Type Nullable Optional Description sourceGPUCopyExternalImageSourceInfo✘ ✘ source image and origin to copy to destination. destinationGPUCopyExternalImageDestInfo✘ ✘ The texture subresource and origin to write to, and its encoding metadata. copySizeGPUExtent3D✘ ✘ Extents of the content to write from source to destination. Returns:
undefinedContent timeline steps:
-
? validate GPUOrigin2D shape(source.
origin). -
? validate GPUOrigin3D shape(destination.
origin). -
? validate GPUExtent3D shape(copySize).
-
Let sourceImage be source.
source -
If sourceImage is not origin-clean, throw a
SecurityErrorand return. -
If any of the following requirements are unmet, throw an
OperationErrorand return.-
source.origin.x + copySize.width must be ≤ the width of sourceImage.
-
source.origin.y + copySize.height must be ≤ the height of sourceImage.
-
copySize.depthOrArrayLayers must be ≤ 1.
-
-
Let usability be ? check the usability of the image argument(source).
-
Issue the subsequent steps on the Device timeline of this.
Device timeline steps:-
Let texture be destination.
texture. -
If any of the following requirements are unmet, generate a validation error and return.
-
usability must be
good. -
texture.
[[destroyed]]must befalse. -
texture must be valid to use with this.
-
validating GPUTexelCopyTextureInfo(destination, copySize) must return
true. -
texture.
usagemust include bothRENDER_ATTACHMENTandCOPY_DST. -
texture.
sampleCountmust be 1. -
texture.
formatmust be one of the following formats (which all supportRENDER_ATTACHMENTusage):
-
-
If copySize.depthOrArrayLayers is > 0, issue the subsequent steps on the Queue timeline of this.
Queue timeline steps:-
Assert that the texel block width of destination.
textureis 1, the texel block height of destination.textureis 1, and that copySize.depthOrArrayLayers is 1. -
Let srcOrigin be source.
origin. -
Let dstOrigin be destination.
origin. -
Let dstSubregion be texture copy sub-region (dstOrigin.z) of destination.
-
For each y in the range [0, copySize.height − 1]:
-
Let srcY be y if source.
flipYisfalseand (copySize.height − 1 − y) otherwise. -
For each x in the range [0, copySize.width − 1]:
-
Set texel block (dstOrigin.x + x, dstOrigin.y + y) of dstSubregion to be an equivalent texel representation of the pixel at (srcOrigin.x + x, srcOrigin.y + srcY) of source.
sourceafter applying any color encoding required by destination.colorSpaceand destination.premultipliedAlpha.
-
-
-
submit(commandBuffers)-
Schedules the execution of the command buffers by the GPU on this queue.
Submitted command buffers cannot be used again.
Called on:GPUQueuethis.Arguments:
Arguments for the GPUQueue.submit(commandBuffers) method. Parameter Type Nullable Optional Description commandBufferssequence<GPUCommandBuffer>✘ ✘ Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this:
Device timeline steps:-
If any of the following requirements are unmet, generate a validation error, invalidate each
GPUCommandBufferin commandBuffers and return.-
Every
GPUCommandBufferin commandBuffers must be valid to use with this. -
Every
GPUCommandBufferin commandBuffers must be unique. -
For each of the following types of resources used by any command in any element of commandBuffers:
GPUBufferb-
b.
[[internal state]]must be "available". GPUTexturet-
t.
[[destroyed]]must befalse. GPUExternalTextureet-
et.
[[expired]]must befalse. GPUQuerySetqs-
qs.
[[destroyed]]must befalse.
Note: For occlusion queries, the
occlusionQuerySetinbeginRenderPass()is not "used" unless it is also used bybeginOcclusionQuery().
-
-
For each commandBuffer in commandBuffers:
-
Invalidate commandBuffer.
-
-
Issue the subsequent steps on the Queue timeline of this:
Queue timeline steps:-
For each commandBuffer in commandBuffers:
-
Execute each command in commandBuffer.
[[command_list]].
-
-
onSubmittedWorkDone()-
Returns a
Promisethat resolves once this queue finishes processing all the work submitted up to this moment.Resolution of this
Promiseimplies the completion ofmapAsync()calls made prior to that call, onGPUBuffers last used exclusively on that queue.Called on:GPUQueuethis.Content timeline steps:
-
Let contentTimeline be the current Content timeline.
-
Let promise be a new promise.
-
Issue the synchronization steps on the Device timeline of this.
-
Return promise.
Device timeline synchronization steps:-
Let event occur upon the completion of all currently-enqueued operations.
-
Listen for timeline event event on this.
[[device]], handled by the subsequent steps on contentTimeline.
Content timeline steps:-
Resolve promise.
-
20. Queries
20.1. GPUQuerySet
[Exposed =(Window ,Worker ),SecureContext ]interface GPUQuerySet {undefined destroy ();readonly attribute GPUQueryType type ;readonly attribute GPUSize32Out count ; };GPUQuerySet includes GPUObjectBase ;
GPUQuerySet
has the following immutable properties:
type, of type GPUQueryType, readonly-
The type of the queries managed by this
GPUQuerySet. count, of type GPUSize32Out, readonly-
The number of queries managed by this
GPUQuerySet.
GPUQuerySet
has the following device timeline properties:
[[destroyed]], of typeboolean, initiallyfalse-
If the query set is destroyed, it can no longer be used in any operation, and its underlying memory can be freed.
20.1.1. QuerySet Creation
A GPUQuerySetDescriptor
specifies the options to use in creating a GPUQuerySet.
dictionary :GPUQuerySetDescriptor GPUObjectDescriptorBase {required GPUQueryType type ;required GPUSize32 count ; };
type, of type GPUQueryType-
The type of queries managed by
GPUQuerySet. count, of type GPUSize32-
The number of queries managed by
GPUQuerySet.
createQuerySet(descriptor)-
Creates a
GPUQuerySet.Called on:GPUDevicethis.Arguments:
Arguments for the GPUDevice.createQuerySet(descriptor) method. Parameter Type Nullable Optional Description descriptorGPUQuerySetDescriptor✘ ✘ Description of the GPUQuerySetto create.Returns:
GPUQuerySetContent timeline steps:
-
If descriptor.
typeis"timestamp", but"timestamp-query"is not enabled for this:-
Throw a
TypeError.
-
-
Let q be ! create a new WebGPU object(this,
GPUQuerySet, descriptor). -
Issue the initialization steps on the Device timeline of this.
-
Return q.
Device timeline initialization steps:-
If any of the following requirements are unmet, generate a validation error, invalidate q and return.
-
Create a device allocation for q where each entry in the query set is zero.
If the allocation fails without side-effects, generate an out-of-memory error, invalidate q, and return.
-
GPUQuerySet
which holds 32 occlusion query results.
const querySet= gpuDevice. createQuerySet({ type: 'occlusion' , count: 32 });
20.1.2. Query Set Destruction
An application that no longer requires a GPUQuerySet
can choose to lose access to it before
garbage collection by calling destroy().
GPUQuerySet
has the following methods:
destroy()-
Destroys the
GPUQuerySet.Called on:GPUQuerySetthis.Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the device timeline.
Device timeline steps:-
Set this.
[[destroyed]]totrue.
-
20.2. QueryType
enum {GPUQueryType ,"occlusion" , };"timestamp"
20.3. Occlusion Query
Occlusion query is only available on render passes, to query the number of fragment samples that pass all the per-fragment tests for a set of drawing commands, including scissor, sample mask, alpha to coverage, stencil, and depth tests. Any non-zero result value for the query indicates that at least one sample passed the tests and reached the output merging stage of the render pipeline, 0 indicates that no samples passed the tests.
When beginning a render pass, GPURenderPassDescriptor.occlusionQuerySet
must be set to be able to use occlusion queries during the pass. An occlusion query is begun
and ended by calling beginOcclusionQuery()
and
endOcclusionQuery()
in pairs that cannot be nested, and resolved into a
GPUBuffer
as a 64-bit unsigned integer by GPUCommandEncoder.resolveQuerySet().
20.4. Timestamp Query
Timestamp queries allow applications to write timestamps to a GPUQuerySet,
using:
and then resolve timestamp values (in nanoseconds as a 64-bit unsigned
integer) into
a GPUBuffer,
using GPUCommandEncoder.resolveQuerySet().
Timestamp values are implementation-defined and may not increase monotonically. The physical device may reset the timestamp counter occasionally, which can result in unexpected values such as negative deltas between timestamps that logically should be monotonically increasing. These instances should be rare and can safely be ignored. Applications should not be written in such a way that unexpected timestamps cause an application failure.
Timestamp queries are implemented using high-resolution timers (see § 2.1.7.2 Device/queue-timeline timing). To mitigate security and privacy concerns, their precision must be reduced:
-
Let fineTimestamp be the current timestamp value of the current queue timeline, in nanoseconds, relative to an implementation-defined point in the past.
-
Return the result of calling coarsen time on fineTimestamp with
crossOriginIsolatedCapabilityset tofalse.
Note: Since cross-origin isolation may not apply to
the device timeline
or
queue timeline,
crossOriginIsolatedCapability is never set to true.
Arguments:
-
GPUDevicedevice -
(timestampWritesGPUComputePassTimestampWritesorGPURenderPassTimestampWrites)
Device timeline steps:
-
Return
trueif the following requirements are met, andfalseif not:-
"timestamp-query"must be enabled for device. -
timestampWrites.
querySetmust be valid to use with device. -
timestampWrites.
querySet.typemust be"timestamp". -
Of the write index members in timestampWrites (
beginningOfPassWriteIndex,endOfPassWriteIndex):
-
21. Canvas Rendering
21.1. HTMLCanvasElement.getContext()
A GPUCanvasContext
object is created
via the getContext()
method of an HTMLCanvasElement
instance by passing the string literal 'webgpu' as its contextType argument.
GPUCanvasContext
from an offscreen HTMLCanvasElement:
const canvas= document. createElement( 'canvas' ); const context= canvas. getContext( 'webgpu' );
Unlike WebGL or 2D context creation, the second argument of
HTMLCanvasElement.getContext()
or
OffscreenCanvas.getContext(),
the context creation attribute dictionary options, is ignored.
Instead, use GPUCanvasContext.configure(),
which allows changing the canvas configuration without replacing the canvas.
HTMLCanvasElement
or OffscreenCanvas)
canvas, run the following
content timeline
steps:
-
Let context be a new
GPUCanvasContext. -
Set context.
canvasto canvas. -
Replace the drawing buffer of context.
-
Return context.
Note: User agents should consider issuing
developer-visible warnings when
an ignored options argument is provided when calling getContext()
to get a WebGPU canvas context.
21.2. GPUCanvasContext
[Exposed =(Window ,Worker ),SecureContext ]interface {GPUCanvasContext readonly attribute (HTMLCanvasElement or OffscreenCanvas )canvas ;undefined configure (GPUCanvasConfiguration configuration );undefined unconfigure ();GPUCanvasConfiguration ?getConfiguration ();GPUTexture getCurrentTexture (); };
GPUCanvasContext
has the following content timeline properties:
canvas, of type(HTMLCanvasElement or OffscreenCanvas), readonly-
The canvas this context was created from.
[[configuration]], of typeGPUCanvasConfiguration?, initiallynull-
The options this context is currently configured with.
nullif the context has not been configured or has beenunconfigured. [[textureDescriptor]], of typeGPUTextureDescriptor?, initiallynull-
The currently configured texture descriptor, derived from the
[[configuration]]and canvas.nullif the context has not been configured or has beenunconfigured. [[drawingBuffer]], an image, initially a transparent black image with the same size as the canvas-
The drawing buffer is the working-copy image data of the canvas. It is exposed as writable by
[[currentTexture]](returned bygetCurrentTexture()).The drawing buffer is used to get a copy of the image contents of a context, which occurs when the canvas is displayed or otherwise read. It may be transparent, even if
[[configuration]].alphaModeis"opaque". ThealphaModeonly affects the result of the "get a copy of the image contents of a context" algorithm.The drawing buffer outlives the
[[currentTexture]]and contains the previously-rendered contents even after the canvas has been presented. It is only cleared in Replace the drawing buffer.Any time the drawing buffer is read, implementations must ensure that all previously submitted work (e.g. queue submissions) have completed writing to it via
[[currentTexture]]. [[currentTexture]], of typeGPUTexture?, initiallynull-
The
GPUTextureto draw into for the current frame. It exposes a writable view onto the underlying[[drawingBuffer]].getCurrentTexture()populates this slot ifnull, then returns it.In the steady-state of a visible canvas, any changes to the drawing buffer made through the currentTexture get presented when updating the rendering of a WebGPU canvas. At or before that point, the texture is also destroyed and
[[currentTexture]]is set to tonull, signalling that a new one is to be created by the next call togetCurrentTexture().Destroyingthe currentTexture has no effect on the drawing buffer contents; it only terminates write-access to the drawing buffer early. During the same frame,getCurrentTexture()continues returning the same destroyed texture.Expire the current texture sets the currentTexture to
null. It is called byconfigure(), resizing the canvas, presentation,transferToImageBitmap(), and others. [[lastPresentedImage]], of type(readonly image)?, initiallynull-
The image most recently presented for this canvas in "updating the rendering of a WebGPU canvas". If the device is lost or destroyed, this image may be used as a fallback in "get a copy of the image contents of a context" in order to prevent the canvas from going blank.
Note: This property only needs to exist in implementations which implement the fallback, which is optional.
GPUCanvasContext
has the following methods:
configure(configuration)-
Configures the context for this canvas. This clears the drawing buffer to transparent black (in Replace the drawing buffer).
Called on:GPUCanvasContextthis.Arguments:
Arguments for the GPUCanvasContext.configure(configuration) method. Parameter Type Nullable Optional Description configurationGPUCanvasConfiguration✘ ✘ Desired configuration for the context. Returns: undefined
Content timeline steps:
-
Let device be configuration.
device. -
? Validate texture format required features of configuration.
formatwith device.[[device]]. -
? Validate texture format required features of each element of configuration.
viewFormatswith device.[[device]]. -
If Supported context formats does not contain configuration.
format, throw aTypeError. -
Let descriptor be the GPUTextureDescriptor for the canvas and configuration(this.
canvas, configuration). -
Set this.
[[configuration]]to configuration.NOTE:This spec requires supporting HDR via thetoneMappingoption. If a user agent only supportstoneMapping: "standard", then thetoneMappingmember should not exist inGPUCanvasConfiguration, so it will not exist on the object returned bygetConfiguration()and will not be accessed byconfigure()). This allows websites to detect feature support. -
Set this.
[[textureDescriptor]]to descriptor. -
Replace the drawing buffer of this.
-
Issue the subsequent steps on the Device timeline of device.
Device timeline steps:-
If any of the following requirements are unmet, generate a validation error and return.
-
validating GPUTextureDescriptor(device, descriptor) must return true.
Note: This early validation remains valid until the next
configure()call, except for validation of thesize, which changes when the canvas is resized. -
-
unconfigure()-
Removes the context configuration. Destroys any textures produced while configured.
Called on:GPUCanvasContextthis.Returns: undefined
Content timeline steps:
-
Set this.
[[configuration]]tonull. -
Set this.
[[textureDescriptor]]tonull. -
Replace the drawing buffer of this.
-
getConfiguration()-
Returns the context configuration.
Called on:GPUCanvasContextthis.Returns:
GPUCanvasConfigurationornullContent timeline steps:
-
Let configuration be a copy of this.
[[configuration]]. -
Return configuration.
NOTE:In scenarios wheregetConfiguration()shows thattoneMappingis implemented and the dynamic-range media query indicates HDR support, then WebGPU canvas should render content using the full HDR range instead of clamping values to the SDR range of the HDR display. -
getCurrentTexture()-
Get the
GPUTexturethat will be composited to the document by theGPUCanvasContextnext.NOTE:An application should callgetCurrentTexture()in the same task that renders to the canvas texture. Otherwise, the texture could get destroyed by these steps before the application is finished rendering to it.The expiry task (defined below) is optional to implement. Even if implemented, task source priority is not normatively defined, so may happen as early as the next task, or as late as after all other task sources are empty (see automatic expiry task source). Expiry is only guaranteed when a visible canvas is displayed (updating the rendering of a WebGPU canvas) and in other callers of "Expire the current texture".
Called on:GPUCanvasContextthis.Returns:
GPUTextureContent timeline steps:
-
If this.
[[configuration]]isnull, throw anInvalidStateErrorand return. -
Assert this.
[[textureDescriptor]]is notnull. -
Let device be this.
[[configuration]].device. -
If this.
[[currentTexture]]isnull:-
Replace the drawing buffer of this.
-
Set this.
[[currentTexture]]to the result of calling device.createTexture()with this.[[textureDescriptor]], except with theGPUTexture’s underlying storage pointing to this.[[drawingBuffer]].Note: If the texture can’t be created (e.g. due to validation failure or out-of-memory), this generates and error and returns an invalidated
GPUTexture. Some validation here is redundant with that done inconfigure(). Implementations must not skip this redundant validation.
-
-
Optionally, queue an automatic expiry task with device device and the following steps:
-
Expire the current texture of this.
Note: If this already happened when updating the rendering of a WebGPU canvas, it has no effect.
-
-
Return this.
[[currentTexture]].
Note: The same
GPUTextureobject will be returned by every call togetCurrentTexture()until "Expire the current texture" runs, even if thatGPUTextureis destroyed, failed validation, or failed to allocate. -
Arguments:
-
context: the
GPUCanvasContext
Returns: image contents
Content timeline steps:
-
Let snapshot be a transparent black image of the same size as context.
canvas. -
Let configuration be context.
[[configuration]]. -
If configuration is
null:-
Return snapshot.
Note: The configuration will be
nullif the context has not been configured or has beenunconfigured. This is identical to the behavior when the canvas has no context. -
-
Ensure that all submitted work items (e.g. queue submissions) have completed writing to the image (via context.
[[currentTexture]]). -
If configuration.
deviceis found to be valid:-
Set snapshot to a copy of the context.
[[drawingBuffer]].
Else, if context.
[[lastPresentedImage]]is notnull:-
Optionally, set snapshot to a copy of context.
[[lastPresentedImage]].Note: This is optional because the
[[lastPresentedImage]]may no longer exist, depending on what caused device loss. Implementations may choose to skip it even if do they still have access to that image.
-
-
Let alphaMode be configuration.
alphaMode. -
- If alphaMode is
"opaque": -
-
Clear the alpha channel of snapshot to 1.0.
-
Tag snapshot as being opaque.
Note: If the
[[currentTexture]], if any, has been destroyed (for example in "Expire the current texture"), the alpha channel is unobservable, and implementations may clear the alpha channel in-place. -
- Otherwise:
-
Tag snapshot with alphaMode.
- If alphaMode is
-
Tag snapshot with the
colorSpaceandtoneMappingof configuration. -
Return snapshot.
GPUCanvasContext
context, run
the following content
timeline steps:
-
Expire the current texture of context.
-
Let configuration be context.
[[configuration]]. -
Set context.
[[drawingBuffer]]to a transparent black image of the same size as context.canvas.-
If configuration is null, the drawing buffer is tagged with the color space
"srgb". In this case, the drawing buffer will remain blank until the context is configured. -
If not, the drawing buffer has the specified configuration.
formatand is tagged with the specified configuration.colorSpaceand configuration.toneMapping.
Note: configuration.
alphaModeis ignored until "get a copy of the image contents of a context".NOTE:A newly replaced drawing buffer image behaves as if it is cleared to transparent black, but, like after"discard", an implementation can clear it lazily only if it becomes necessary.Note: This will often be a no-op, if the drawing buffer is already cleared and has the correct configuration.
-
GPUCanvasContext
context, run
the following content
timeline steps:
-
If context.
[[currentTexture]]is notnull:-
Call context.
[[currentTexture]].destroy()(without destroying context.[[drawingBuffer]]) to terminate write access to the image. -
Set context.
[[currentTexture]]tonull.
-
21.3. HTML Specification Hooks
The following algorithms "hook" into algorithms in the HTML specification, and must run at the specified points.
HTMLCanvasElement
or OffscreenCanvas
with a
GPUCanvasContext
context, run the following content timeline steps:
-
Return a copy of the image contents of context.
-
When an
HTMLCanvasElementhas its rendering updated.-
Including when the canvas is the placeholder canvas element of an
OffscreenCanvas.
-
-
When
transferToImageBitmap()creates anImageBitmapfrom the bitmap. (See also transferToImageBitmap from WebGPU.) -
When WebGPU canvas contents are read using other Web APIs, like
drawImage(),texImage2D(),texSubImage2D(),toDataURL(),toBlob(), and so on.
If alphaMode
is "opaque",
this incurs a clear of the alpha channel. Implementations may skip this step when
they are able to read or display images in a way that ignores the alpha channel.
If an application needs a canvas only for interop (not presentation), avoid
"opaque"
if it is not needed.
HTMLCanvasElement
or an OffscreenCanvas
with a placeholder canvas element)
with a GPUCanvasContext
context, which occurs before getting the canvas’s image contents,
in the following sub-steps of the event loop processing model:
-
"update the rendering or user interface of that
Document" -
"update the rendering of that dedicated worker"
Note:
Service and Shared workers do not have "update the rendering" steps
because they cannot render to user-visible canvases.
requestAnimationFrame()
is not exposed in
ServiceWorkerGlobalScope
and SharedWorkerGlobalScope,
and
OffscreenCanvases
from transferControlToOffscreen()
cannot be sent to these workers.
Run the following content timeline steps:
-
Expire the current texture of context.
Note: If this already happened in the task queued by
getCurrentTexture(), it has no effect. -
Set context.
[[lastPresentedImage]]to context.[[drawingBuffer]].Note: This is just a reference, not a copy; the drawing buffer’s contents can’t change in-place after the current texture has expired.
Note:
This does not happen for standalone OffscreenCanvases
(created by new OffscreenCanvas()).
When transferToImageBitmap()
is called on a canvas with
GPUCanvasContext
context, after creating an ImageBitmap
from the canvas’s bitmap,
run the following content timeline steps:
-
Replace the drawing buffer of context.
Note: This makes transferToImageBitmap()
equivalent to "moving" (and possibly alpha-clearing) the image contents into the
ImageBitmap, without a copy.
-
The update the canvas size algorithm.
21.4. GPUCanvasConfiguration
The supported
context formats are the set of GPUTextureFormats:
«"bgra8unorm",
"rgba8unorm",
"rgba16float"».
These formats must be supported when specified as a
GPUCanvasConfiguration.format
regardless of the given
GPUCanvasConfiguration.device.
Note: Canvas configuration cannot use srgb
formats like "bgra8unorm-srgb".
Instead, use the non-srgb equivalent ("bgra8unorm"),
specify the srgb
format in the viewFormats,
and use createView()
to create
a view with an srgb format.
enum GPUCanvasAlphaMode {"opaque" ,"premultiplied" , };enum GPUCanvasToneMappingMode {"standard" ,"extended" , };dictionary {GPUCanvasToneMapping GPUCanvasToneMappingMode = "standard"; };mode dictionary {GPUCanvasConfiguration required GPUDevice device ;required GPUTextureFormat format ;GPUTextureUsageFlags usage = 0x10; // GPUTextureUsage.RENDER_ATTACHMENTsequence <GPUTextureFormat >viewFormats = [];PredefinedColorSpace colorSpace = "srgb";GPUCanvasToneMapping toneMapping = {};GPUCanvasAlphaMode alphaMode = "opaque"; };
GPUCanvasConfiguration
has the following members:
device, of type GPUDevice-
The
GPUDevicethat textures returned bygetCurrentTexture()will be compatible with. format, of type GPUTextureFormat-
The format that textures returned by
getCurrentTexture()will have. Must be one of the Supported context formats. usage, of type GPUTextureUsageFlags, defaulting to0x10-
The usage that textures returned by
getCurrentTexture()will have.RENDER_ATTACHMENTis the default, but is not automatically included if the usage is explicitly set. Be sure to includeRENDER_ATTACHMENTwhen setting a custom usage if you wish to use textures returned bygetCurrentTexture()as color targets for a render pass. viewFormats, of type sequence<GPUTextureFormat>, defaulting to[]-
The formats that views created from textures returned by
getCurrentTexture()may use. colorSpace, of type PredefinedColorSpace, defaulting to"srgb"-
The color space that values written into textures returned by
getCurrentTexture()should be displayed with. toneMapping, of type GPUCanvasToneMapping, defaulting to{}-
The tone mapping determines how the content of textures returned by
getCurrentTexture()are to be displayed.Note: If an implementation doesn’t support HDR WebGPU canvases, it should also not expose this member, to allow for feature detection. See
getConfiguration(). alphaMode, of type GPUCanvasAlphaMode, defaulting to"opaque"-
Determines the effect that alpha values will have on the content of textures returned by
getCurrentTexture()when read, displayed, or used as an image source.
GPUCanvasContext
to be used with a specific GPUDevice,
using the preferred
format for this context:
const canvas= document. createElement( 'canvas' ); const context= canvas. getContext( 'webgpu' ); context. configure({ device: gpuDevice, format: navigator. gpu. getPreferredCanvasFormat(), });
HTMLCanvasElement
or OffscreenCanvas)
canvas,
GPUCanvasConfiguration
configuration)
is a GPUTextureDescriptor
with the following members:
-
size: [canvas.width, canvas.height, 1]. -
viewFormats: configuration.viewFormats.
and other members set to their defaults.
canvas.width refers to HTMLCanvasElement.width
or OffscreenCanvas.width.
canvas.height refers to HTMLCanvasElement.height
or OffscreenCanvas.height.
21.4.1. Canvas Color Space
During presentation, the color values in the canvas are converted to the color space of the screen.
The toneMapping
determines the handling of values
outside of the [0, 1] interval in the color space of the screen.
21.4.2. Canvas Context sizing
All canvas configuration is set in configure()
except for the resolution
of the canvas, which is set by the canvas’s width and height.
Note: Like WebGL and 2d canvas, resizing a WebGPU canvas loses the current contents of the drawing buffer. In WebGPU, it does so by replacing the drawing buffer.
HTMLCanvasElement
or OffscreenCanvas
canvas with a
GPUCanvasContext
context has its width or height attributes set,
update the canvas size by running the following
content timeline
steps:
-
Replace the drawing buffer of context.
-
Let configuration be context.
[[configuration]] -
If configuration is not
null:-
Set context.
[[textureDescriptor]]to the GPUTextureDescriptor for the canvas and configuration(canvas, configuration).
-
Note: This may result in a GPUTextureDescriptor
which exceeds the
maxTextureDimension2D
of the device. In this case,
validation will fail inside getCurrentTexture().
Note: This algorithm is run any time the
canvas width or height attributes are set, even
if their value is not changed.
21.5. GPUCanvasToneMappingMode
This enum specifies how color values are displayed to the screen.
"standard"-
Color values within the standard dynamic range of the screen are unchanged, and all other color values are projected to the standard dynamic range of the screen.
Note: This projection is often accomplished by clamping color values in the color space of the screen to the
[0, 1]interval.For example, suppose that the value(1.035, -0.175, -0.140)is written to an'srgb'canvas.If this is presented to an sRGB screen, then this will be converted to sRGB (which is a no-op, because the canvas is sRGB), then projected into the display’s space. Using component-wise clamping, this results in the sRGB value
(1.0, 0.0, 0.0).If this is presented to a Display P3 screen, then this will be converted to the value
(0.948, 0.106, 0.01)in the Display P3 color space, and no clamping will be needed. "extended"-
Color values in the extended dynamic range of the screen are unchanged, and all other color values are projected to the extended dynamic range of the screen.
Note: This projection is often accomplished by clamping color values in the color space of the screen to the interval of values that the screen is capable of displaying, which may include values greater than
1.For example, suppose that the value(2.5, -0.15, -0.15)is written to an'srgb'canvas.If this is presented to an sRGB screen that is capable of displaying values in the
[0, 4]interval in sRGB space, then this will be converted to sRGB (which is a no-op, because the canvas is sRGB), then projected into the display’s space. If using component-wise clamping, this results in the sRGB value(2.5, 0.0, 0.0).If this is presented to a Display P3 screen that is capable of displaying values in the
[0, 2]interval in Display P3 space, then this will be converted to the value(2.3, 0.545, 0.386)in the Display P3 color space, then projected into the display’s space. If using component-wise clamping, this results in the Display P3 value(2.0, 0.545, 0.386).
21.6. GPUCanvasAlphaMode
This enum selects how the contents of the canvas will be interpreted when read, when displayed to the screen or used as an image source (in drawImage, toDataURL, etc.)
Below, src is a value in the canvas texture, and dst is an image that the canvas
is being composited into (e.g. an HTML page rendering, or a 2D canvas).
"opaque"-
Read RGB as opaque and ignore alpha values. If the content is not already opaque, the alpha channel is cleared to 1.0 in "get a copy of the image contents of a context".
"premultiplied"-
Read RGBA as premultiplied: color values are premultiplied by their alpha value. 100% red at 50% alpha is
[0.5, 0, 0, 0.5].If the canvas texture contains out-of-gamut premultiplied RGBA values at the time the canvas contents are read, the behavior depends on whether the canvas is:
- used as an image source
-
Values are preserved, as described in color space conversion.
- displayed to the screen
-
Compositing results are undefined.
Note: This is true even if color space conversion would produce in-gamut values before compositing, because the intermediate format for compositing is not specified.
22. Errors & Debugging
During the normal course of operation of WebGPU, errors are raised via dispatch error.
After a device is lost, errors are no longer surfaced, where possible. After this point, implementations do not need to run validation or error tracking:
-
The validity of objects on the device becomes unobservable.
-
popErrorScope()anduncapturederrorstop reporting errors. (No errors are generated by the device loss itself. Instead, theGPUDevice.lostpromise resolves to indicate the device is lost.) -
All operations which send a message back to the content timeline will skip their usual steps. Most will appear to succeed, except for
mapAsync(), which produces an error because it is impossible to provide the correct mapped data after the device has been lost.This makes it unobservable whether other types of operations (that don’t send messages back) actually execute or not.
22.1. Fatal Errors
enum {GPUDeviceLostReason ,"unknown" , }; ["destroyed" Exposed =(Window ,Worker ),SecureContext ]interface {GPUDeviceLostInfo readonly attribute GPUDeviceLostReason ;reason readonly attribute DOMString ; };message partial interface GPUDevice {readonly attribute Promise <GPUDeviceLostInfo >lost ; };
GPUDevice has
the following additional attributes:
lost, of type Promise<GPUDeviceLostInfo>, readonly-
A slot-backed attribute holding a promise which is created with the device, remains pending for the lifetime of the device, then resolves when the device is lost.
Upon initialization, it is set to a new promise.
22.2. GPUError
[Exposed =(Window ,Worker ),SecureContext ]interface GPUError {readonly attribute DOMString message ; };
GPUError is the
base interface for all errors surfaced from popErrorScope()
and the uncapturederror
event.
Errors must only be generated for operations that explicitly state the conditions one may be generated under in their respective algorithms, and the subtype of error that is generated.
No errors are generated from a device which is lost. See § 22 Errors & Debugging.
Note: GPUError may gain
new subtypes in future versions of this spec. Applications should handle
this possibility, using only the error’s message
when possible, and specializing using
instanceof. Use error.constructor.name when it’s necessary to serialize an error
(e.g. into
JSON, for a debug report).
GPUError has the
following immutable
properties:
message, of type DOMString, readonly-
A human-readable, localizable text message providing information about the error that occurred.
Note: This message is generally intended for application developers to debug their applications and capture information for debug reports, not to be surfaced to end-users.
Note: User agents should not include potentially machine-parsable details in this message, such as free system memory on
"out-of-memory"or other details about the conditions under which memory was exhausted.Note: The
messageshould follow the best practices for language and direction information. This includes making use of any future standards which may emerge regarding the reporting of string language and direction metadata.Editorial note: At the time of this writing, no language/direction recommendation is available that provides compatibility and consistency with legacy APIs, but when there is, adopt it formally.
[Exposed =(Window ,Worker ),SecureContext ]interface :GPUValidationError GPUError {(constructor DOMString ); };message
GPUValidationError
is a subtype of GPUError which
indicates that an operation did not
satisfy all validation requirements. Validation errors are always indicative of an application
error, and is expected to fail the same way across all devices assuming the same
[[features]]
and [[limits]]
are in use.
GPUDevice
device, run the following steps:
Device timeline steps:
-
Let error be a new
GPUValidationErrorwith an appropriate error message. -
Dispatch error error to device.
[Exposed =(Window ,Worker ),SecureContext ]interface :GPUOutOfMemoryError GPUError {(constructor DOMString ); };message
GPUOutOfMemoryError
is a subtype of GPUError which
indicates that there was not enough free
memory to complete the requested operation. The operation may succeed if attempted again with a
lower memory requirement (like using smaller texture dimensions), or if memory used by other
resources is released first.
GPUDevice
device, run the following steps:
Device timeline steps:
-
Let error be a new
GPUOutOfMemoryErrorwith an appropriate error message. -
Dispatch error error to device.
[Exposed =(Window ,Worker ),SecureContext ]interface :GPUInternalError GPUError {(constructor DOMString ); };message
GPUInternalError
is a subtype of GPUError which
indicates than an operation failed for a
system or implementation-specific reason even when all validation requirements have been satisfied.
For example, the operation may exceed the capabilities of the implementation in a way not easily
captured by the supported
limits. The same operation may succeed on other devices or under
difference circumstances.
GPUDevice
device, run the following steps:
Device timeline steps:
-
Let error be a new
GPUInternalErrorwith an appropriate error message. -
Dispatch error error to device.
22.3. Error Scopes
A GPU error scope
captures GPUErrors that
were generated while the
GPU error scope was
current. Error scopes are used to isolate errors that occur within a set
of WebGPU calls, typically for debugging purposes or to make an operation more fault tolerant.
GPU error scope has the following device timeline properties:
[[errors]], of type list<GPUError>, initially []-
The
GPUErrors, if any, observed while the GPU error scope was current. [[filter]], of typeGPUErrorFilter-
Determines what type of
GPUErrorthis GPU error scope observes.
enum {GPUErrorFilter "validation" ,"out-of-memory" ,"internal" , };partial interface GPUDevice {undefined pushErrorScope (GPUErrorFilter filter );Promise <GPUError ?>popErrorScope (); };
GPUErrorFilter
defines the type of errors that should be caught when calling
pushErrorScope():
"validation"-
Indicates that the error scope will catch a
GPUValidationError. "out-of-memory"-
Indicates that the error scope will catch a
GPUOutOfMemoryError. "internal"-
Indicates that the error scope will catch a
GPUInternalError.
GPUDevice has
the following device timeline properties:
[[errorScopeStack]], of type stack<GPU error scope>-
A stack of GPU error scopes that have been pushed to the
GPUDevice.
GPUError
error and GPUDevice
device is determined by issuing the following steps to the device timeline of device:
Device timeline steps:
-
If error is an instance of:
GPUValidationError-
Let type be "validation".
GPUOutOfMemoryError-
Let type be "out-of-memory".
GPUInternalError-
Let type be "internal".
-
Let scope be the last item of device.
[[errorScopeStack]]. -
While scope is not
undefined:-
If scope.
[[filter]]is type, return scope. -
Set scope to the previous item of device.
[[errorScopeStack]].
-
-
Return
undefined.
GPUError
error on GPUDevice
device, run the following device timeline steps:
Note: No errors are generated from a device which is lost. If this algorithm is called while device is lost, it will not be observable to the application. See § 22 Errors & Debugging.
-
Let scope be the current error scope for error and device.
-
If scope is not
undefined:-
Append error to scope.
[[errors]]. -
Return.
-
-
Otherwise issue the following steps to the content timeline:
-
If the user agent chooses, queue a global task for GPUDevice device with the following steps:
-
Fire a
GPUUncapturedErrorEventnamed "uncapturederror" on device, with anerrorof error.
-
Note: After dispatching the event, user agents
should surface uncaptured errors to
developers, for example as warnings in the browser’s developer console, unless the event’s
defaultPrevented
is true. In other words, calling preventDefault()
on the event should silence the console warning.
Note: The user agent may choose to throttle or limit
the number of GPUUncapturedErrorEvents
that a GPUDevice
can raise to prevent an excessive amount of error handling or logging from
impacting performance.
pushErrorScope(filter)-
Pushes a new GPU error scope onto the
[[errorScopeStack]]for this.Called on:GPUDevicethis.Arguments:
Arguments for the GPUDevice.pushErrorScope(filter) method. Parameter Type Nullable Optional Description filterGPUErrorFilter✘ ✘ Which class of errors this error scope observes. Returns:
undefinedContent timeline steps:
-
Issue the subsequent steps on the Device timeline of this.
Device timeline steps:-
Let scope be a new GPU error scope.
-
Set scope.
[[filter]]to filter. -
Push scope onto this.
[[errorScopeStack]].
-
popErrorScope()-
Pops a GPU error scope off the
[[errorScopeStack]]for this and resolves to anyGPUErrorobserved by the error scope, ornullif none.There is no guarantee of the ordering of promise resolution.
Called on:GPUDevicethis.Content timeline steps:
-
Let contentTimeline be the current Content timeline.
-
Let promise be a new promise.
-
Issue the check steps on the Device timeline of this.
-
Return promise.
Device timeline check steps:-
If this is lost:
-
Issue the following steps on contentTimeline:
Content timeline steps:-
Resolve promise with
null.
-
-
Return.
Note: No errors are generated from a device which is lost. See § 22 Errors & Debugging.
-
-
If any of the following requirements are unmet:
-
this.
[[errorScopeStack]].size must be > 0.
Then issue the following steps on contentTimeline and return:
Content timeline steps:-
Reject promise with an
OperationError.
-
-
Let scope be the result of popping an item off of this.
[[errorScopeStack]]. -
Let error be any one of the items in scope.
[[errors]], ornullif there are none.For any two errors E1 and E2 in the list, if E2 was caused by E1, E2 should not be the one selected.
Note: For example, if E1 comes from
t=createTexture(), and E2 comes fromt.createView()becausetwas invalid, E1 should be be preferred since it will be easier for a developer to understand what went wrong. Since both of these areGPUValidationErrors, the only difference will be in themessagefield, which is meant only to be read by humans anyway. -
At an unspecified point now or in the future, issue the subsequent steps on contentTimeline.
Note: By allowing
popErrorScope()calls to resolve in any order, with any of the errors observed by the scope, this spec allows validation to complete out of order, as long as any state observations are made at the appropriate point in adherence to this spec. For example, this allows implementations to perform shader compilation, which depends only on non-stateful inputs, to be completed on a background thread in parallel with other device-timeline work, and report any resulting errors later.
Content timeline steps:-
Resolve promise with error.
-
GPUDevice
operation that may fail:
gpuDevice. pushErrorScope( 'validation' ); let sampler= gpuDevice. createSampler({ maxAnisotropy: 0 , // Invalid, maxAnisotropy must be at least 1. }); gpuDevice. popErrorScope(). then(( error) => { if ( error) { // There was an error creating the sampler, so discard it. sampler= null ; console. error( `An error occured while creating sampler: ${ error. message} ` ); } });
For example: An error scope that only contains the creation of a single resource, such as a texture or buffer, can be used to detect failures such as out of memory conditions, in which case the application may try freeing some resources and trying the allocation again.
Error scopes do not identify which command failed, however. So, for instance, wrapping all the commands executed while loading a model in a single error scope will not offer enough granularity to determine if the issue was due to memory constraints. As a result freeing resources would usually not be a productive response to a failure of that scope. A more appropriate response would be to allow the application to fall back to a different model or produce a warning that the model could not be loaded. If responding to memory constraints is desired, the operations allocating memory can always be wrapped in a smaller nested error scope.
22.4. Telemetry
When a GPUError
is generated that is not observed by any GPU error scope, the user agent may fire an event named uncapturederror at a GPUDevice
using GPUUncapturedErrorEvent.
Note: uncapturederror
events are intended to be used for telemetry and reporting
unexpected errors. They may not be dispatched for all uncaptured errors (for example, there may be a limit
on the number of errors surfaced), and should not be used for handling known error cases that may occur
during
normal operation of an application. Prefer using pushErrorScope()
and
popErrorScope()
in those cases.
[Exposed =(Window ,Worker ),SecureContext ]interface :GPUUncapturedErrorEvent Event {(constructor DOMString ,type GPUUncapturedErrorEventInit ); [gpuUncapturedErrorEventInitDict SameObject ]readonly attribute GPUError error ; };dictionary :GPUUncapturedErrorEventInit EventInit {required GPUError ; };error
GPUUncapturedErrorEvent
has the following attributes:
error, of type GPUError, readonly-
A slot-backed attribute holding an object representing the error that was uncaptured. This has the same type as errors returned by
popErrorScope().
partial interface GPUDevice {attribute EventHandler onuncapturederror ; };
GPUDevice has
the following content timeline properties:
onuncapturederror, of type EventHandler-
An event handler IDL attribute for the
uncapturederrorevent type.
GPUDevice:
gpuDevice. addEventListener( 'uncapturederror' , ( event) => { // Re-surface the error, because adding an event listener may silence console logs. console. error( 'A WebGPU error was not captured:' , event. error); myEngineDebugReport. uncapturedErrors. push({ type: event. error. constructor . name, message: event. error. message, }); });
23. Detailed Operations
This section describes the details of various GPU operations.
23.1. Computing
Computing operations provide direct access to GPU’s programmable hardware.
Compute shaders do not have shader stage inputs or outputs; their results are
side effects from writing data into storage bindings bound either as
GPUBufferBindingLayout
with GPUBufferBindingType
"storage"
or as GPUStorageTextureBindingLayout.
These operations are encoded within GPUComputePassEncoder
as:
The main compute algorithm:
Arguments:
-
descriptor: Description of the current
GPUComputePipeline. -
dispatchCall: The dispatch call parameters. May come from function arguments or an
INDIRECTbuffer.
-
Let computeStage be descriptor.
compute. -
Let workgroupSize be the computed workgroup size for computeStage.
entryPointafter applying computeStage.constantsto computeStage.module. -
For workgroupX in range
[0, dispatchCall.:workgroupCountX]-
For workgroupY in range
[0, dispatchCall.:workgroupCountY]-
For workgroupZ in range
[0, dispatchCall.:workgroupCountZ]-
For localX in range
[0, workgroupSize.:x]-
For localY in range
[0, workgroupSize.:y]-
For localZ in range
[0, workgroupSize.:y]-
Let invocation be
{ computeStage, workgroupX, workgroupY, workgroupZ, localX, localY, localZ } -
Append invocation to computeInvocations.
-
-
-
-
-
-
-
For every invocation in computeInvocations, in any order the device chooses, including in parallel:
-
Set the shader builtins:
-
Set the num_workgroups builtin, if any, to
(
dispatchCall.workgroupCountX,
dispatchCall.workgroupCountY,
dispatchCall.workgroupCountZ
) -
Set the workgroup_id builtin, if any, to
(
invocation.workgroupX,
invocation.workgroupY,
invocation.workgroupZ
) -
Set the local_invocation_id builtin, if any, to
(
invocation.localX,
invocation.localY,
invocation.localZ
) -
Set the global_invocation_id builtin, if any, to
(.
invocation.workgroupX * workgroupSize.x+ invocation.localX,
invocation.workgroupY * workgroupSize.y+ invocation.localY,
invocation.workgroupZ * workgroupSize.z+ invocation.localZ
) -
Set the local_invocation_index builtin, if any, to
invocation.localX + (invocation.localY * workgroupSize.x) + (invocation.localZ * workgroupSize.x* workgroupSize.y)
-
-
Invoke the compute shader entry point described by invocation.computeStage.
-
Note: Shader invocations have no guaranteed order, and will generally run in parallel according to device capabilities. Developers should not assume that any given invocation or workgroup will complete before any other one is started. Some devices may appear to execute in a consistent order, but this behavior should not be relied on as it will not perform identically across all devices. Shaders that require synchronization across invocations must use Synchronization Built-in Functions to coordinate execution.
The device may become lost if shader execution does not end in a reasonable amount of time, as determined by the user agent.
23.2. Rendering
Rendering is done by a set of GPU operations that are executed within GPURenderPassEncoder,
and result in modifications of the texture data, viewed by the render pass attachments.
These operations are encoded with:
Note: rendering is the traditional use of GPUs, and is supported by multiple fixed-function blocks in hardware.
The main rendering algorithm:
Arguments:
-
pipeline: The current
GPURenderPipeline. -
drawCall: The draw call parameters. May come from function arguments or an
INDIRECTbuffer. -
state: RenderState of the
GPURenderCommandsMixinwhere the draw call is issued.
-
Let descriptor be pipeline.
[[descriptor]]. -
Resolve indices. See § 23.2.1 Index Resolution.
Let vertexList be the result of resolve indices(drawCall, state).
-
Process vertices. See § 23.2.2 Vertex Processing.
Execute process vertices(vertexList, drawCall, descriptor.
vertex, state). -
Assemble primitives. See § 23.2.3 Primitive Assembly.
Execute assemble primitives(vertexList, drawCall, descriptor.
primitive). -
Clip primitives. See § 23.2.4 Primitive Clipping.
Let primitiveList be the result of this stage.
-
Rasterize. See § 23.2.5 Rasterization.
Let rasterizationList be the result of rasterize(primitiveList, state).
-
Process fragments. See § 23.2.6 Fragment Processing.
Gather a list of fragments, resulting from executing process fragment(rasterPoint, descriptor, state) for each rasterPoint in rasterizationList.
-
Write pixels. See § 23.2.7 Output Merging.
For each non-null fragment of fragments:
-
Execute process depth stencil(fragment, pipeline, state).
-
Execute process color attachments(fragment, pipeline, state).
-
23.2.1. Index Resolution
At the first stage of rendering, the pipeline builds a list of vertices to process for each instance.
Arguments:
-
drawCall: The draw call parameters. May come from function arguments or an
INDIRECTbuffer. -
state: The snapshot of the
GPURenderCommandsMixinstate at the time of the draw call.
Returns: list of integer indices.
-
Let vertexIndexList be an empty list of indices.
-
If drawCall is an indexed draw call:
-
Initialize the vertexIndexList with drawCall.indexCount integers.
-
For i in range 0 .. drawCall.indexCount (non-inclusive):
-
Let relativeVertexIndex be fetch index(i + drawCall.
firstIndex, state.[[index_buffer]]). -
If relativeVertexIndex has the special value
"out of bounds", return the empty list.Note: Implementations may choose to display a warning when this occurs, especially when it is easy to detect (like in non-indirect indexed draw calls).
-
Append drawCall.
baseVertex+ relativeVertexIndex to the vertexIndexList.
-
-
-
Otherwise:
-
Initialize the vertexIndexList with drawCall.vertexCount integers.
-
Set each vertexIndexList item i to the value drawCall.firstVertex + i.
-
-
Return vertexIndexList.
Note: in the case of indirect draw calls, the
indexCount, vertexCount,
and other properties of drawCall are read from the indirect buffer
instead of the draw command itself.
Arguments:
-
i: Index of a vertex index to fetch.
-
state: The snapshot of the
GPURenderCommandsMixinstate at the time of the draw call.
Returns: unsigned integer or "out of bounds"
-
Let indexSize be defined by the state.
[[index_format]]: -
If state.
[[index_buffer_offset]]+ |i + 1| × indexSize > state.[[index_buffer_size]], return the special value"out of bounds". -
Interpret the data in state.
[[index_buffer]], starting at offset state.[[index_buffer_offset]]+ i × indexSize, of size indexSize bytes, as an unsigned integer and return it.
23.2.2. Vertex Processing
Vertex processing stage is a programmable stage of the render pipeline that processes the vertex attribute data, and produces clip space positions for § 23.2.4 Primitive Clipping, as well as other data for the § 23.2.6 Fragment Processing.
Arguments:
-
vertexIndexList: List of vertex indices to process (mutable, passed by reference).
-
drawCall: The draw call parameters. May come from function arguments or an
INDIRECTbuffer. -
desc: The descriptor of type
GPUVertexState. -
state: The snapshot of the
GPURenderCommandsMixinstate at the time of the draw call.
Each vertex vertexIndex in the vertexIndexList,
in each instance of index rawInstanceIndex, is processed independently.
The rawInstanceIndex is in range from 0 to drawCall.instanceCount - 1, inclusive.
This processing happens in parallel, and any side effects, such as
writes into GPUBufferBindingType
"storage"
bindings,
may happen in any order.
-
Let instanceIndex be rawInstanceIndex + drawCall.firstInstance.
-
For each non-
nullvertexBufferLayout in the list of desc.buffers:-
Let i be the index of the buffer layout in this list.
-
Let vertexBuffer, vertexBufferOffset, and vertexBufferBindingSize be the buffer, offset, and size at slot i of state.
[[vertex_buffers]]. -
Let vertexElementIndex be dependent on vertexBufferLayout.
stepMode:"vertex"-
vertexIndex
"instance"-
instanceIndex
-
Let drawCallOutOfBounds be
false. -
For each attributeDesc in vertexBufferLayout.
attributes:-
Let attributeOffset be vertexBufferOffset + vertexElementIndex * vertexBufferLayout.
arrayStride+ attributeDesc.offset. -
If attributeOffset + byteSize(attributeDesc.
format) > vertexBufferOffset + vertexBufferBindingSize:-
Set drawCallOutOfBounds to
true. -
Optionally (implementation-defined), empty vertexIndexList and return, cancelling the draw call.
Note: This allows implementations to detect out-of-bounds values in the index buffer before issuing a draw call, instead of using invalid memory reference behavior.
-
-
-
For each attributeDesc in vertexBufferLayout.
attributes:-
If drawCallOutOfBounds is
true:-
Load the attribute data according to WGSL’s invalid memory reference behavior, from vertexBuffer.
Note: Invalid memory reference allows several behaviors, including actually loading the "correct" result for an attribute that is in-bounds, even when the draw-call-wide drawCallOutOfBounds is
true.
Otherwise:
-
Let attributeOffset be vertexBufferOffset + vertexElementIndex * vertexBufferLayout.
arrayStride+ attributeDesc.offset. -
Load the attribute data of format attributeDesc.
formatfrom vertexBuffer starting at offset attributeOffset. The components are loaded in the orderx,y,z,wfrom buffer memory.
-
-
Convert the data into a shader-visible format, according to channel formats rules.
An attribute of type"snorm8x2"and byte values of[0x70, 0xD0]will be converted tovec2<f32>(0.88, -0.38)in WGSL. -
Adjust the data size to the shader type:
-
if both are scalar, or both are vectors of the same dimensionality, no adjustment is needed.
-
if data is vector but the shader type is scalar, then only the first component is extracted.
-
if both are vectors, and data has a higher dimension, the extra components are dropped.
An attribute of type"float32x3"and valuevec3<f32>(1.0, 2.0, 3.0)will exposed to the shader asvec2<f32>(1.0, 2.0)if a 2-component vector is expected. -
if the shader type is a vector of higher dimensionality, or the data is a scalar, then the missing components are filled from
vec4<*>(0, 0, 0, 1)value.An attribute of type"sint32"and value5will be exposed to the shader asvec4<i32>(5, 0, 0, 1)if a 4-component vector is expected.
-
-
Bind the data to vertex shader input location attributeDesc.
shaderLocation.
-
-
-
For each
GPUBindGroupgroup at index in state.[[bind_groups]]:-
For each resource
GPUBindingResourcein the bind group:-
Let entry be the corresponding
GPUBindGroupLayoutEntryfor this resource. -
If entry.
visibilityincludesVERTEX:-
Bind the resource to the shader under group index and binding
GPUBindGroupLayoutEntry.binding.
-
-
-
-
Set the shader builtins:
-
Set the
vertex_indexbuiltin, if any, to vertexIndex. -
Set the
instance_indexbuiltin, if any, to instanceIndex.
-
-
Invoke the vertex shader entry point described by desc.
Note: The target platform caches the results of vertex shader invocations. There is no guarantee that any vertexIndex that repeats more than once will result in multiple invocations. Similarly, there is no guarantee that a single vertexIndex will only be processed once.
The device may become lost if shader execution does not end in a reasonable amount of time, as determined by the user agent.
23.2.3. Primitive Assembly
Primitives are assembled by a fixed-function stage of GPUs.
Arguments:
-
vertexIndexList: List of vertex indices to process.
-
drawCall: The draw call parameters. May come from function arguments or an
INDIRECTbuffer. -
desc: The descriptor of type
GPUPrimitiveState.
For each instance, the primitives get assembled from the vertices that have been processed by the shaders, based on the vertexIndexList.
-
First, if the primitive topology is a strip, (which means that desc.
stripIndexFormatis not undefined) and the drawCall is indexed, the vertexIndexList is split into sub-lists using the maximum value of desc.stripIndexFormatas a separator.Example: a vertexIndexList with values
[1, 2, 65535, 4, 5, 6]of type"uint16"will be split in sub-lists[1, 2]and[4, 5, 6]. -
For each of the sub-lists vl, primitive generation is done according to the desc.
topology:"line-list"-
Line primitives are composed from (vl.0, vl.1), then (vl.2, vl.3), then (vl.4 to vl.5), etc. Each subsequent primitive takes 2 vertices.
"line-strip"-
Line primitives are composed from (vl.0, vl.1), then (vl.1, vl.2), then (vl.2, vl.3), etc. Each subsequent primitive takes 1 vertex.
"triangle-list"-
Triangle primitives are composed from (vl.0, vl.1, vl.2), then (vl.3, vl.4, vl.5), then (vl.6, vl.7, vl.8), etc. Each subsequent primitive takes 3 vertices.
"triangle-strip"-
Triangle primitives are composed from (vl.0, vl.1, vl.2), then (vl.2, vl.1, vl.3), then (vl.2, vl.3, vl.4), then (vl.4, vl.3, vl.5), etc. Each subsequent primitive takes 1 vertices.
Any incomplete primitives are dropped.
23.2.4. Primitive Clipping
Vertex shaders have to produce a built-in position (of type vec4<f32>),
which denotes the clip
position of a vertex in clip space coordinates.
Primitives are clipped to the clip volume, which, for any clip position p inside a primitive, is defined by the following inequalities:
-
−p.w ≤ p.x ≤ p.w
-
−p.w ≤ p.y ≤ p.w
-
0 ≤ p.z ≤ p.w (depth clipping)
When the "clip-distances"
feature is enabled, this clip
volume can
be further restricted by user-defined half-spaces by declaring clip_distances in the
output of vertex stage. Each value in the clip_distances array will be linearly
interpolated across the primitive, and the portion of the primitive with interpolated distances less
than 0 will be clipped.
If descriptor.primitive.unclippedDepth
is true,
depth clipping is not
applied: the clip volume is not
bounded in the z dimension.
A primitive passes through this stage unchanged if every one of its edges
lie entirely inside the clip
volume.
If the edges of a primitives intersect the boundary of the clip volume,
the intersecting edges are reconnected by new edges that lie along the boundary of the clip volume.
For triangular primitives (descriptor.primitive.topology
is
"triangle-list"
or "triangle-strip"),
this reconnection
may result in introduction of new vertices into the polygon, internally.
If a primitive intersects an edge of the clip volume’s boundary, the clipped polygon must include a point on this boundary edge.
If the vertex shader outputs other floating-point values (scalars and vectors), qualified with "perspective" interpolation, they also get clipped. The output values associated with a vertex that lies within the clip volume are unaffected by clipping. If a primitive is clipped, however, the output values assigned to vertices produced by clipping are clipped.
Considering an edge between vertices a and b that got clipped, resulting in the vertex c, let’s define t to be the ratio between the edge vertices: c.p = t × a.p + (1 − t) × b.p, where x.p is the output clip position of a vertex x.
For each vertex output value "v" with a corresponding fragment input, a.v and b.v would be the outputs for a and b vertices respectively. The clipped shader output c.v is produced based on the interpolation qualifier:
- flat
-
Flat interpolation is unaffected, and is based on the provoking vertex, which is determined by the interpolation sampling mode declared in the shader. The output value is the same for the whole primitive, and matches the vertex output of the provoking vertex.
- linear
-
The interpolation ratio gets adjusted against the perspective coordinates of the clip positions, so that the result of interpolation is linear in screen space.
- perspective
-
The value is linearly interpolated in clip space, producing perspective-correct values.
The result of primitive clipping is a new set of primitives, which are contained within the clip volume.
23.2.5. Rasterization
Rasterization is the hardware processing stage that maps the generated primitives
to the 2-dimensional rendering area of the framebuffer -
the set of render attachments in the current GPURenderPassEncoder.
This rendering area is split into an even grid of pixels.
The framebuffer coordinates start from the top-left corner of the render targets. Each unit corresponds exactly to one pixel. See § 3.3 Coordinate Systems for more information.
Rasterization determines the set of pixels affected by a primitive. In case of multi-sampling,
each pixel is further split into
descriptor.multisample.count
samples.
The standard sample
patterns are as follows,
with positions in framebuffer coordinates relative to the top-left corner of the pixel,
such that the pixel ranges from (0, 0) to (1, 1):
multisample.count
| Sample positions |
|---|---|
| 1 | Sample 0: (0.5, 0.5) |
| 4 |
Sample 0: (0.375, 0.125) Sample 1: (0.875, 0.375) Sample 2: (0.125, 0.625) Sample 3: (0.625, 0.875) |
Implementations must use the standard sample pattern for the given
multisample.count
when performing rasterization.
Let’s define a FragmentDestination to contain:
- position
-
the 2D pixel position using framebuffer coordinates
- sampleIndex
-
an integer in case § 23.2.10 Per-Sample Shading is active, or
nullotherwise
We’ll also use a notion of normalized device coordinates, or NDC. In this coordinate system, the viewport bounds range in X and Y from -1 to 1, and in Z from 0 to 1.
Rasterization produces a list of RasterizationPoints, each containing the following data:
- destination
-
refers to FragmentDestination
- coverageMask
-
refers to multisample coverage mask (see § 23.2.11 Sample Masking)
- frontFacing
-
is true if it’s a point on the front face of a primitive
- perspectiveDivisor
-
refers to interpolated 1.0 ÷ W across the primitive
- depth
-
refers to the depth in viewport coordinates, i.e. between the
[[viewport]]minDepthandmaxDepth. - primitiveVertices
-
refers to the list of vertex outputs forming the primitive
- barycentricCoordinates
-
refers to § 23.2.5.3 Barycentric coordinates
Arguments:
-
primitiveList: List of primitives to rasterize.
-
state: The active RenderState.
Returns: list of RasterizationPoint.
Each primitive in primitiveList is processed independently. However, the order of primitives affects later stages, such as depth/stencil operations and pixel writes.
-
First, the clipped vertices are transformed into NDC - normalized device coordinates. Given the output position p, the NDC position and perspective divisor are:
ndc(p) = vector(p.x ÷ p.w, p.y ÷ p.w, p.z ÷ p.w)
divisor(p) = 1.0 ÷ p.w
-
Let vp be state.
[[viewport]]. Map the NDC position n into viewport coordinates:-
Compute framebuffer coordinates from the render target offset and size:
framebufferCoords(n) = vector(vp.
x+ 0.5 × (n.x + 1) × vp.width, vp.y+ 0.5 × (−n.y + 1) × vp.height) -
Compute depth by linearly mapping [0,1] to the viewport depth range:
depth(n) = vp.
minDepth+ n.z× ( vp.maxDepth- vp.minDepth)
-
-
Let rasterizationPoints be the list of points, each having its attributes (
divisor(p),framebufferCoords(n),depth(n), etc.) interpolated according to its position on the primitive, using the same interpolation as § 23.2.4 Primitive Clipping. If the attribute is user-defined (not a built-in output value) then the interpolation type specified by the @interpolate WGSL attribute is used. -
Proceed with a specific rasterization algorithm, depending on
primitive.topology:"point-list"-
The point, if not filtered by § 23.2.4 Primitive Clipping, goes into § 23.2.5.1 Point Rasterization.
"line-list"or"line-strip"-
The line cut by § 23.2.4 Primitive Clipping goes into § 23.2.5.2 Line Rasterization.
"triangle-list"or"triangle-strip"-
The polygon produced in § 23.2.4 Primitive Clipping goes into § 23.2.5.4 Polygon Rasterization.
-
Remove all the points rp from rasterizationPoints that have rp.destination.position outside of state.
[[scissorRect]]. -
Return rasterizationPoints.
23.2.5.1. Point Rasterization
A single FragmentDestination is selected within the pixel containing the framebuffer coordinates of the point.
The coverage mask depends on multi-sampling mode:
- sample-frequency
-
coverageMask = 1 ≪
sampleIndex - pixel-frequency multi-sampling
-
coverageMask = 1 ≪ descriptor.
multisample.count− 1 - no multi-sampling
-
coverageMask = 1
23.2.5.2. Line Rasterization
The exact algorithm used for line rasterization is not defined, and may differ between implementations. For example, the line may be drawn using § 23.2.5.4 Polygon Rasterization of a 1px-width rectangle around the line segment, or using Bresenham’s line algorithm to select the FragmentDestinations.
Note: See Basic Line Segment Rasterization and Bresenham Line Segment Rasterization in the Vulkan 1.3 spec for more details of how line these line rasterization algorithms may be implemented.
23.2.5.3. Barycentric coordinates
Barycentric coordinates is a list of n numbers bi, defined for a point p inside a convex polygon with n vertices vi in framebuffer space. Each bi is in range 0 to 1, inclusive, and represents the proximity to vertex vi. Their sum is always constant:
∑ (bi) = 1
These coordinates uniquely specify any point p within the polygon (or on its boundary) as:
p = ∑ (bi × pi)
For a polygon with 3 vertices - a triangle, barycentric coordinates of any point p can be computed as follows:
Apolygon = A(v1, v2, v3) b1 = A(p, b2, b3) ÷ Apolygon b2 = A(b1, p, b3) ÷ Apolygon b3 = A(b1, b2, p) ÷ Apolygon
Where A(list of points) is the area of the polygon with the given set of vertices.
For polygons with more than 3 vertices, the exact algorithm is implementation-dependent. One of the possible implementations is to triangulate the polygon and compute the barycentrics of a point based on the triangle it falls into.
23.2.5.4. Polygon Rasterization
A polygon is front-facing if it’s oriented towards the projection. Otherwise, the polygon is back-facing.
Arguments:
Returns: list of RasterizationPoint.
-
Let rasterizationPoints be an empty list.
-
Let v(i) be the framebuffer coordinates for the clipped vertex number i (starting with 1) in a rasterized polygon of n vertices.
Note: this section uses the term "polygon" instead of a "triangle", since § 23.2.4 Primitive Clipping stage may have introduced additional vertices. This is non-observable by the application.
-
Determine if the polygon is front-facing, which depends on the sign of the area occupied by the polygon in framebuffer coordinates:
area = 0.5 × ((v1.x × vn.y − vn.x × v1.y) + ∑ (vi+1.x × vi.y − vi.x × vi+1.y))
The sign of area is interpreted based on the
primitive.frontFace:"ccw"-
area > 0 is considered front-facing, otherwise back-facing
"cw"-
area < 0 is considered front-facing, otherwise back-facing
-
Cull based on
primitive.cullMode:"none"-
All polygons pass this test.
"front"-
The front-facing polygons are discarded, and do not process in later stages of the render pipeline.
"back"-
The back-facing polygons are discarded.
-
Determine a set of fragments inside the polygon in framebuffer space - these are locations scheduled for the per-fragment operations. This operation is known as "point sampling". The logic is based on descriptor.
multisample:- disabled
-
Fragments are associated with pixel centers. That is, all the points with coordinates C, where fract(C) = vector2(0.5, 0.5) in the framebuffer space, enclosed into the polygon, are included. If a pixel center is on the edge of the polygon, whether or not it’s included is not defined.
Note: this becomes a subject of precision for the rasterizer.
- enabled
-
Each pixel is associated with descriptor.
multisample.countlocations, which are implementation-defined. The locations are ordered, and the list is the same for each pixel of the framebuffer. Each location corresponds to one fragment in the multisampled framebuffer.The rasterizer builds a mask of locations being hit inside each pixel and provides is as "sample-mask" built-in to the fragment shader.
-
For each produced fragment of type FragmentDestination:
-
Let rp be a new RasterizationPoint object
-
Compute the list b as § 23.2.5.3 Barycentric coordinates of that fragment. Set rp.barycentricCoordinates to b.
-
Let di be the depth value of vi.
-
Set rp.depth to ∑ (bi × di)
-
Append rp to rasterizationPoints.
-
-
Return rasterizationPoints.
23.2.6. Fragment Processing
The fragment processing stage is a programmable stage of the render pipeline that computes the fragment data (often a color) to be written into render targets.
This stage produces a Fragment for each RasterizationPoint:
-
destination refers to FragmentDestination.
-
frontFacing is true if it’s a fragment on the front face of a primitive.
-
coverageMask refers to multisample coverage mask (see § 23.2.11 Sample Masking).
-
depth refers to the depth in viewport coordinates, i.e. between the
[[viewport]]minDepthandmaxDepth. -
colors refers to the list of color values, one for each target in
colorAttachments. -
depthPassed is
trueif the fragment passed thedepthCompareoperation. -
stencilPassed is
trueif the fragment passed the stencilcompareoperation.
Arguments:
-
rp: The RasterizationPoint, produced by § 23.2.5 Rasterization.
-
descriptor: The descriptor of type
GPURenderPipelineDescriptor. -
state: The active RenderState.
Returns: Fragment or
null.
-
Let fragmentDesc be descriptor.
fragment. -
Let depthStencilDesc be descriptor.
depthStencil. -
Let fragment be a new Fragment object.
-
Set fragment.destination to rp.destination.
-
Set fragment.frontFacing to rp.frontFacing.
-
Set fragment.coverageMask to rp.coverageMask.
-
If
frag_depthbuiltin is not produced by the shader:-
Set fragment.depthPassed to the result of compare fragment(fragment.destination, fragment.depth, "depth", state.
[[depthStencilAttachment]], depthStencilDesc?.depthCompare).
-
-
Set stencilState to depthStencilDesc?.
stencilFrontif rp.frontFacing istrueand depthStencilDesc?.stencilBackotherwise. -
Set fragment.stencilPassed to the result of compare fragment(fragment.destination, state.
[[stencilReference]], "stencil", state.[[depthStencilAttachment]], stencilState?.compare). -
If fragmentDesc is not
null:-
If fragment.depthPassed is
false, thefrag_depthbuiltin is not produced by the shader entry point, and the shader entry point does not write to any storage bindings, the following steps may be skipped. -
Set the shader input builtins. For each non-composite argument of the entry point, annotated as a builtin, set its value based on the annotation:
position-
vec4<f32>(rp.destination.position, rp.depth, rp.perspectiveDivisor) front_facing-
rp.frontFacing
sample_indexsample_mask-
rp.coverageMask
-
For each user-specified shader stage input of the fragment stage:
-
Let value be the interpolated fragment input, based on rp.barycentricCoordinates, rp.primitiveVertices, and the interpolation qualifier on the input.
-
Set the corresponding fragment shader location input to value.
-
-
Invoke the fragment shader entry point described by fragmentDesc.
The device may become lost if shader execution does not end in a reasonable amount of time, as determined by the user agent.
-
If the fragment issued
discard, returnnull. -
Set fragment.colors to the user-specified shader stage output values from the shader.
-
Take the shader output builtins:
-
If
frag_depthbuiltin is produced by the shader as value:-
Let vp be state.
[[viewport]]. -
Set fragment.depth to clamp(value, vp.
minDepth, vp.maxDepth). -
Set fragment.depthPassed to the result of compare fragment(fragment.destination, fragment.depth, "depth", state.
[[depthStencilAttachment]], depthStencilDesc?.depthCompare).
-
-
-
If
sample_maskbuiltin is produced by the shader as value:-
Set fragment.coverageMask to fragment.coverageMask ∧ value.
-
Otherwise we are in § 23.2.8 No Color Output mode, and fragment.colors is empty.
-
-
Return fragment.
Arguments:
-
destination: The FragmentDestination.
-
value: The value to be compared.
-
aspect: The aspect of attachment to sample values from.
-
attachment: The attachment to be compared against.
-
compareFunc: The
GPUCompareFunctionto use, orundefined.
Returns: true if the comparison passes, or false otherwise
-
If attachment is
undefinedor does not have aspect, returntrue. -
If compareFunc is
undefinedor"always", returntrue. -
Let attachmentValue be the value of aspect of attachment at destination.
-
Return
trueif comparing value with attachmentValue using compareFunc succeeds, andfalseotherwise.
Processing of fragments happens in parallel, while any side effects,
such as writes into GPUBufferBindingType
"storage"
bindings,
may happen in any order.
23.2.7. Output Merging
Output merging is a fixed-function stage of the render pipeline that outputs the fragment color, depth and stencil data to be written into the render pass attachments.
Arguments:
-
fragment: The Fragment, produced by § 23.2.6 Fragment Processing.
-
pipeline: The current
GPURenderPipeline. -
state: The active RenderState.
-
Let depthStencilDesc be pipeline.
[[descriptor]].depthStencil. -
If pipeline.
[[writesDepth]]istrueand fragment.depthPassed istrue:-
Set the value of the depth aspect of state.
[[depthStencilAttachment]]at fragment.destination to fragment.depth.
-
-
If pipeline.
[[writesStencil]]is true:-
Set stencilState to depthStencilDesc.
stencilFrontif fragment.frontFacing istrueand depthStencilDesc.stencilBackotherwise. -
If fragment.stencilPassed is
false:-
Let stencilOp be stencilState.
failOp.
Else if fragment.depthPassed is
false:-
Let stencilOp be stencilState.
depthFailOp.
Else:
-
Let stencilOp be stencilState.
passOp.
-
-
Update the value of the stencil aspect of state.
[[depthStencilAttachment]]at fragment.destination by performing the operation described by stencilOp.
-
The depth input to this stage, if any, is clamped to the current [[viewport]]
depth
range (regardless of whether the fragment shader stage writes the frag_depth builtin).
Arguments:
-
fragment: The Fragment, produced by § 23.2.6 Fragment Processing.
-
pipeline: The current
GPURenderPipeline. -
state: The active RenderState.
-
If fragment.depthPassed is
falseor fragment.stencilPassed isfalse, return. -
Let targets be pipeline.
[[descriptor]].fragment.targets. -
For each attachment of state.
[[colorAttachments]]:-
Let color be the value from fragment.colors that corresponds with attachment.
-
Let targetDesc be the targets entry that corresponds with attachment.
-
If targetDesc.
blendis provided:-
Set the RGB components of color to the value computed by performing the operation described by colorBlend.
operationwith the values described by colorBlend.srcFactorand colorBlend.dstFactor. -
Set the alpha component of color to the value computed by performing the operation described by alphaBlend.
operationwith the values described by alphaBlend.srcFactorand alphaBlend.dstFactor.
-
Set the value of attachment at fragment.destination to color.
-
23.2.8. No Color Output
In no-color-output mode, pipeline does not produce any color attachment outputs.
The pipeline still performs rasterization and produces depth values based on the vertex position output. The depth testing and stencil operations can still be used.
23.2.9. Alpha to Coverage
In alpha-to-coverage mode, an additional alpha-to-coverage mask
of MSAA samples is generated based on the alpha component of the
fragment shader output value at @location(0).
The algorithm of producing the extra mask is platform-dependent and can vary for different pixels. It guarantees that:
-
if alpha ≤ 0.0, the result is 0x0
-
if alpha ≥ 1.0, the result is 0xFFFFFFFF
-
intermediate alpha values should result in a proportionate number of bits set to 1 in the mask. Some platforms may not guarantee that the number of bits set to 1 in the mask monotonically increases as alpha increases for a given pixel.
23.2.10. Per-Sample Shading
When rendering into multisampled render attachments, fragment shaders can be run once per-pixel or once
per-sample.
Fragment shaders must run once per-sample if either the sample_index builtin or sample interpolation sampling
is used and contributes to the shader output. Otherwise fragment shaders may run once
per-pixel with the result
broadcast out to each of the samples included in the final sample mask.
When using per-sample shading, the color output for sample N is produced by the fragment shader
execution
with sample_index == N for the current pixel.
23.2.11. Sample Masking
The final sample mask
for a pixel is computed as:
rasterization mask
& mask
& shader-output
mask.
Only the lower count
bits of the mask are considered.
If the least-significant bit at position N of the final sample mask has value of "0", the sample color outputs (corresponding to sample N) to all attachments of the fragment shader are discarded. Also, no depth test or stencil operations are executed on the relevant samples of the depth-stencil attachment.
The rasterization mask is produced by the rasterization stage, based on the shape of the rasterized polygon. The samples included in the shape get the relevant bits 1 in the mask.
The shader-output
mask takes the output value of "sample_mask" builtin
in the fragment shader.
If the builtin is not output from the fragment shader, and alphaToCoverageEnabled
is enabled, the shader-output mask becomes the alpha-to-coverage mask.
Otherwise, it defaults to 0xFFFFFFFF.
24. Type Definitions
typedef [EnforceRange ]unsigned long ;GPUBufferDynamicOffset typedef [EnforceRange ]unsigned long ;GPUStencilValue typedef [EnforceRange ]unsigned long ;GPUSampleMask typedef [EnforceRange ]long ;GPUDepthBias typedef [EnforceRange ]unsigned long long ;GPUSize64 typedef [EnforceRange ]unsigned long ;GPUIntegerCoordinate typedef [EnforceRange ]unsigned long ;GPUIndex32 typedef [EnforceRange ]unsigned long ;GPUSize32 typedef [EnforceRange ]long ;GPUSignedOffset32 typedef unsigned long long ;GPUSize64Out typedef unsigned long ;GPUIntegerCoordinateOut typedef unsigned long ;GPUSize32Out typedef unsigned long ;GPUFlagsConstant
24.1. Colors & Vectors
dictionary {GPUColorDict required double r ;required double g ;required double b ;required double a ; };typedef (sequence <double >or GPUColorDict );GPUColor
Note: double is large enough to precisely
hold 32-bit signed/unsigned
integers and single-precision floats.
r, of type double-
The red channel value.
g, of type double-
The green channel value.
b, of type double-
The blue channel value.
a, of type double-
The alpha channel value.
GPUColor
value color, depending on its type, the syntax:
-
color.r refers to either
GPUColorDict.ror the first item of the sequence (asserting there is such an item). -
color.g refers to either
GPUColorDict.gor the second item of the sequence (asserting there is such an item). -
color.b refers to either
GPUColorDict.bor the third item of the sequence (asserting there is such an item). -
color.a refers to either
GPUColorDict.aor the fourth item of the sequence (asserting there is such an item).
Arguments:
-
color: The
GPUColorto validate.
Returns: undefined
Content timeline steps:
dictionary {GPUOrigin2DDict GPUIntegerCoordinate = 0;x GPUIntegerCoordinate = 0; };y typedef (sequence <GPUIntegerCoordinate >or GPUOrigin2DDict );GPUOrigin2D
GPUOrigin2D
value origin, depending on its type, the syntax:
-
origin.x refers to either
GPUOrigin2DDict.xor the first item of the sequence (0 if not present). -
origin.y refers to either
GPUOrigin2DDict.yor the second item of the sequence (0 if not present).
Arguments:
-
origin: The
GPUOrigin2Dto validate.
Returns: undefined
Content timeline steps:
dictionary {GPUOrigin3DDict GPUIntegerCoordinate = 0;x GPUIntegerCoordinate = 0;y GPUIntegerCoordinate = 0; };z typedef (sequence <GPUIntegerCoordinate >or GPUOrigin3DDict );GPUOrigin3D
GPUOrigin3D
value origin, depending on its type, the syntax:
-
origin.x refers to either
GPUOrigin3DDict.xor the first item of the sequence (0 if not present). -
origin.y refers to either
GPUOrigin3DDict.yor the second item of the sequence (0 if not present). -
origin.z refers to either
GPUOrigin3DDict.zor the third item of the sequence (0 if not present).
Arguments:
-
origin: The
GPUOrigin3Dto validate.
Returns: undefined
Content timeline steps:
dictionary {GPUExtent3DDict required GPUIntegerCoordinate width ;GPUIntegerCoordinate height = 1;GPUIntegerCoordinate depthOrArrayLayers = 1; };typedef (sequence <GPUIntegerCoordinate >or GPUExtent3DDict );GPUExtent3D
width, of type GPUIntegerCoordinate-
The width of the extent.
height, of type GPUIntegerCoordinate, defaulting to1-
The height of the extent.
depthOrArrayLayers, of type GPUIntegerCoordinate, defaulting to1-
The depth of the extent or the number of array layers it contains. If used with a
GPUTexturewith aGPUTextureDimensionof"3d"defines the depth of the texture. If used with aGPUTexturewith aGPUTextureDimensionof"2d"defines the number of array layers in the texture.
GPUExtent3D
value extent, depending on its type, the syntax:
-
extent.width refers to either
GPUExtent3DDict.widthor the first item of the sequence (asserting there is such an item). -
extent.height refers to either
GPUExtent3DDict.heightor the second item of the sequence (1 if not present). -
extent.depthOrArrayLayers refers to either
GPUExtent3DDict.depthOrArrayLayersor the third item of the sequence (1 if not present).
Arguments:
-
extent: The
GPUExtent3Dto validate.
Returns: undefined
Content timeline steps:
-
Throw a
TypeErrorif:
25. Feature Index
25.1. "core-features-and-limits"
Allows all Core WebGPU features and limits to be used.
Note: This is currently available on all adapters and enabled automatically on all devices even if not requested.
25.2. "depth-clip-control"
Allows depth clipping to be disabled.
This feature adds the following optional API surfaces:
-
New
GPUPrimitiveStatedictionary members:
25.3. "depth32float-stencil8"
Allows for explicit creation of textures of format "depth32float-stencil8".
This feature adds the following optional API surfaces:
-
New
GPUTextureFormatenum values:
25.4. "texture-compression-bc"
Allows for explicit creation of textures of BC compressed formats which include the "S3TC", "RGTC", and "BPTC" formats. Only supports 2D textures.
Note: Adapters which support "texture-compression-bc"
do not
always support "texture-compression-bc-sliced-3d".
To use "texture-compression-bc-sliced-3d",
"texture-compression-bc"
must be enabled explicitly as this feature
does not enable the BC formats.
This feature adds the following optional API surfaces:
-
New
GPUTextureFormatenum values:
25.5. "texture-compression-bc-sliced-3d"
Allows the 3d
dimension for textures with BC compressed formats.
Note: Adapters which support "texture-compression-bc"
do not
always support "texture-compression-bc-sliced-3d".
To use "texture-compression-bc-sliced-3d",
"texture-compression-bc"
must be enabled explicitly as this feature
does not enable the BC formats.
This feature adds no optional API surfaces.
25.6. "texture-compression-etc2"
Allows for explicit creation of textures of ETC2 compressed formats. Only supports 2D textures.
This feature adds the following optional API surfaces:
-
New
GPUTextureFormatenum values:
25.7. "texture-compression-astc"
Allows for explicit creation of textures of ASTC compressed formats. Only supports 2D textures.
This feature adds the following optional API surfaces:
-
New
GPUTextureFormatenum values:
25.8. "texture-compression-astc-sliced-3d"
Allows the 3d
dimension for textures with ASTC compressed formats.
Note: Adapters which support "texture-compression-astc"
do not
always support "texture-compression-astc-sliced-3d".
To use "texture-compression-astc-sliced-3d",
"texture-compression-astc"
must be enabled explicitly as this feature
does not enable the ASTC formats.
This feature adds no optional API surfaces.
25.9. "timestamp-query"
Adds the ability to query timestamps from GPU command buffers. See § 20.4 Timestamp Query.
This feature adds the following optional API surfaces:
-
New
GPUQueryTypevalues: -
New
GPUComputePassDescriptormembers: -
New
GPURenderPassDescriptormembers:
25.10. "indirect-first-instance"
Allows the use of non-zero firstInstance values in indirect draw parameters and
indirect drawIndexed parameters.
This feature adds no optional API surfaces.
25.11.
"shader-f16"
Allows the use of the half-precision floating-point type f16 in WGSL.
This feature adds the following optional API surfaces:
-
New WGSL extensions:
25.12. "rg11b10ufloat-renderable"
Allows the RENDER_ATTACHMENT
usage on textures with format "rg11b10ufloat",
and also allows textures of that format to be blended, multisampled, and resolved.
This feature adds no optional API surfaces.
Enabling "texture-formats-tier1"
at device creation will also enable
"rg11b10ufloat-renderable".
25.13. "bgra8unorm-storage"
Allows the STORAGE_BINDING
usage on textures with format "bgra8unorm".
This feature adds no optional API surfaces.
25.14. "float32-filterable"
Makes textures with formats "r32float",
"rg32float",
and
"rgba32float"
filterable.
25.15. "float32-blendable"
Makes textures with formats "r32float",
"rg32float",
and
"rgba32float"
blendable.
25.16. "clip-distances"
Allows the use of clip_distances in WGSL.
This feature adds the following optional API surfaces:
-
New WGSL extensions:
25.17. "dual-source-blending"
Allows the use of blend_src in WGSL and simultaneously using both pixel shader
outputs
(@blend_src(0) and @blend_src(1)) as inputs to a blending operation with the
single color
attachment at location 0.
This feature adds the following optional API surfaces:
-
Allows the use of the below
GPUBlendFactors: -
New WGSL extensions:
25.18.
"subgroups"
Allows the use of the subgroup and quad operations in WGSL.
This feature adds no optional API surfaces, but the following entries of GPUAdapterInfo
expose real values whenever the feature is available on the adapter:
-
New WGSL extensions:
25.19. "texture-formats-tier1"
Supports the below new GPUTextureFormats
with the RENDER_ATTACHMENT,
blendable, multisampling
capabilities and the STORAGE_BINDING
capability
with the "read-only"
and "write-only"
GPUStorageTextureAccesses:
Allows the RENDER_ATTACHMENT,
blendable, multisampling
and resolve
capabilities on below GPUTextureFormats:
Allows the "read-only"
or "write-only"
GPUStorageTextureAccess
on below GPUTextureFormats:
Enabling "texture-formats-tier2"
at device creation will also enable
"texture-formats-tier1".
Enabling "texture-formats-tier1"
at device creation will also enable
"rg11b10ufloat-renderable".
25.20. "texture-formats-tier2"
Allows the "read-write"
GPUStorageTextureAccess
on below
GPUTextureFormats:
Enabling "texture-formats-tier2"
at device creation will also enable
"texture-formats-tier1".
26. Appendices
26.1. Texture Format Capabilities
26.1.1. Plain color formats
All supported plain color
formats support usages
COPY_SRC,
COPY_DST,
and
TEXTURE_BINDING,
and dimension "3d".
The RENDER_ATTACHMENT
and STORAGE_BINDING
columns
specify support for GPUTextureUsage.RENDER_ATTACHMENT
and GPUTextureUsage.STORAGE_BINDING
usage respectively.
The render
target pixel byte cost
and render
target component alignment
are used to validate the maxColorAttachmentBytesPerSample
limit.
Note: The texel block memory cost of each of these formats is the same as its texel block copy footprint.
26.1.2. Depth-stencil formats
A depth-or-stencil format is any format with depth and/or stencil aspects. A combined depth-stencil format is a depth-or-stencil format that has both depth and stencil aspects.
All depth-or-stencil formats support the COPY_SRC,
COPY_DST,
TEXTURE_BINDING,
and RENDER_ATTACHMENT
usages.
All of these formats support multisampling.
However, certain copy operations also restrict the source and destination formats, and none of
these formats support textures with "3d"
dimension.
Depth textures cannot be used with "filtering"
samplers, but can always
be used with "comparison"
samplers even if they use filtering.
| Format |
NOTE:
Texel block memory cost (Bytes)
| Aspect | GPUTextureSampleType
| Valid texel copy source | Valid texel copy destination | Texel block copy footprint (Bytes) | Aspect-specific format |
|---|---|---|---|---|---|---|---|
stencil8
| 1 − 4 | stencil | "uint"
| ✓ | 1 | stencil8
| |
depth16unorm
| 2 | depth | "depth",
"unfilterable-float"
| ✓ | 2 | depth16unorm
| |
depth24plus
| 4 | depth | "depth",
"unfilterable-float"
| ✗ | – | depth24plus
| |
depth24plus-stencil8
| 4 − 8 | depth | "depth",
"unfilterable-float"
| ✗ | – | depth24plus
| |
| stencil | "uint"
| ✓ | 1 | stencil8
| |||
depth32float
| 4 | depth | "depth",
"unfilterable-float"
| ✓ | ✗ | 4 | depth32float
|
depth32float-stencil8
| 5 − 8 | depth | "depth",
"unfilterable-float"
| ✓ | ✗ | 4 | depth32float
|
| stencil | "uint"
| ✓ | 1 | stencil8
| |||
24-bit depth refers to a 24-bit unsigned normalized depth format with a range from 0.0 to 1.0, which would be spelled "depth24unorm" if exposed.
26.1.2.1. Reading and Sampling Depth/Stencil Textures
It is possible to bind a depth-aspect GPUTextureView
to either a texture_depth_* binding or a binding with other non-depth 2d/cube texture types.
A stencil-aspect GPUTextureView
must be bound to a normal texture binding type.
The sampleType
in the GPUBindGroupLayout
must be "uint".
Reading or sampling the depth or stencil aspect of a texture behaves as if the texture contains
the values (V, X, X, X), where V is the actual depth or stencil value,
and each X is an implementation-defined unspecified value.
For depth-aspect bindings, the unspecified values are not visible through bindings with
texture_depth_* types.
tex with type texture_2d<f32>:
-
textureSample(tex, ...)will returnvec4<f32>(D, X, X, X). -
textureGather(0, tex, ...)will returnvec4<f32>(D1, D2, D3, D4). -
textureGather(2, tex, ...)will returnvec4<f32>(X1, X2, X3, X4)(a completely unspecified value).
Note:
Short of adding a new more constrained stencil sampler type (like depth), it’s infeasible for
implementations to efficiently paper over the driver differences for depth/stencil reads.
As this was not a portability pain point for WebGL, it’s not expected to be problematic in WebGPU.
In practice, expect either (V, V, V, V) or (V, 0, 0, 1) (where V is
the depth or stencil
value), depending on hardware.
26.1.2.2. Copying Depth/Stencil Textures
The depth aspects of depth32float formats
("depth32float"
and "depth32float-stencil8"
have a limited range.
As a result, copies into such textures are only valid from other textures of the same format.
The depth aspects of depth24plus formats
("depth24plus"
and "depth24plus-stencil8")
have opaque representations (implemented as either 24-bit depth or "depth32float").
As a result, depth-aspect texel
copies are not allowed with these formats.
-
All of these formats can be written in a render pass using a fragment shader that outputs depth values via the
frag_depthoutput. -
Textures with "depth24plus" formats can be read as shader textures, and written to a texture (as a render pass attachment) or buffer (via a storage buffer binding in a compute shader).
26.1.3. Packed formats
All packed texture formats support COPY_SRC,
COPY_DST,
and TEXTURE_BINDING
usages.
All of these formats are filterable.
None of these formats are renderable
or support multisampling.
A compressed format is any format with a block size greater than 1×1.
Note: The texel block memory cost of each of these formats is the same as its texel block copy footprint.